iOS 8上的输入点击时不显示DatePicker

时间:2014-10-27 06:28:39

标签: ios iphone uidatepicker uiactionsheet

我在输入字段上显示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上测试。

1 个答案:

答案 0 :(得分:0)

他们更改了iOS 8中UIActionSheet的实现。您无法再向其视图层次结构添加视图

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

  

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

此外,iOS 8中的UIActionSheet 已弃用

  

重要提示: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