【问题标题】:DatePicker does not show on input click on iOS 8DatePicker 不显示在 iOS 8 上的输入点击
【发布时间】:2023-03-14 04:20:02
【问题描述】:

我在输入字段上显示 pickerView 单击它在其他 iOS 上运行良好,但是当我更新到 iOS 8 时它不起作用。当我单击输入字段时,它不会显示日期选择器。

这是我正在使用的代码。

- (void)textFieldDidBeginEditing:(UITextField *)aTextField{

     appDelegate.recentAddSowID = @"";

     if (aTextField == dateTextField) {
     [dateTextField resignFirstResponder];

     pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:nil];

     pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
     pickerView.datePickerMode=UIDatePickerModeDate;

     pickerView.hidden = NO;
     pickerView.date = [NSDate date];

     UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
     pickerToolbar.barStyle = UIBarStyleBlackOpaque;
     [pickerToolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];

      UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
     [barItems addObject:flexSpace];

      UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
      [barItems addObject:doneBtn];

      UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
      [barItems addObject:cancelBtn];

       [pickerToolbar setItems:barItems animated:YES];

       [pickerViewPopup addSubview:pickerToolbar];
       [pickerViewPopup addSubview:pickerView];

       [pickerView addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

       [dateTextField setInputView:pickerView];

       [pickerViewPopup showInView:self.view];
       [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
    }
}

我正在 iPhone 5S 上进行测试。

【问题讨论】:

  • 设置断点。该方法是否被调用?
  • 你检查设备了吗?对我来说,它不会出现在模拟器中,但在设备上可以正常工作
  • @Daiji-Djan 方法被称为它在 ios 7 上的模拟器上运行良好
  • @Shanti K 是的,它适用于模拟器,但不适用于设备,因为模拟器是 iOS7
  • 我不是这个意思。如果它不起作用 - 它是否被调用。在ios8上

标签: ios iphone uidatepicker uiactionsheet


【解决方案1】:

他们在 iOS 8 中更改了 UIActionSheet 的实现。您不能再将视图添加到其视图层次结构中

文档 (https://developer.apple.com/library/ios/documentation/uikit/Reference/UIActionSheet_Class/index.html) 指出:

UIActionSheet 并非设计为子类化,也不应将视图添加到其层次结构中。如果您需要呈现比 UIActionSheet API 提供的自定义更多的表单,您可以创建自己的表单并使用 presentViewController:animated:completion: 以模态方式呈现它。

此外,UIActionSheet 在 iOS 8 中已弃用

重要提示:UIActionSheet 在 iOS 8 中已弃用。(请注意,UIActionSheetDelegate 也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请改用 UIAlertController 和 UIAlertControllerStyleActionSheet 的首选样式。

或者,试试这个:https://github.com/skywinder/ActionSheetPicker-3.0。它适用于 iOS 8,可能是您所需要的

来源:Add UIPickerView in UIActionSheet from IOS 8 not working

【讨论】:

  • 对不起,我觉得你运气不好。这是升级到 iOS 8 的痛苦的一部分 :(
猜你喜欢
  • 2021-04-10
  • 2019-06-22
  • 2020-01-05
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多