【问题标题】:How to Add a UIDatePicker to UIALertView in iOS7如何在 iOS7 中将 UIDatePicker 添加到 UIALertView
【发布时间】:2013-09-29 07:22:56
【问题描述】:

在我的一种观点中,我已经实现了 datepicker, 方法是将日期选择器添加到Alertview。现在在 iOS 7 中,我无法在警报视图上找到日期选择器,当我搜索到时,我们无法在 iOS7 中向警报视图添加任何内容。

我的要求是当我选择 DOB 文本字段时,我必须显示 Datepickerview 并修改日期,更改的值将存储到文本字段中。

现在我的问题是如何在警报视图上显示日期选择器。

我已经尝试在视图上显示,但是当试图从视图中删除 datpicker 时它不起作用。

【问题讨论】:

    标签: iphone ios datepicker uialertview ios7


    【解决方案1】:

    我知道这是一个较老的问题,但我相信以下允许 OP 要求的内容,并且我认为不会违反 Apple 涉及 AlertView 层次结构更改的规则:

    - (void)requestDateOfBirth
    {
    UIDatePicker *birthDatePicker;
    UIAlertView *setBirthDate;
    NSDateFormatter *birthDateFormatter;
    UITextField *birthDateInput;
    //create the alertview
    setBirthDate = [[UIAlertView alloc] initWithTitle:@"Date of Birth:"
                                                  message:nil
                                                 delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"OK", nil];
    setBirthDate.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    //create the datepicker
    birthDatePicker = [[UIDatePicker alloc] init];
    [birthDatePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    birthDatePicker.datePickerMode = UIDatePickerModeDate;    
    birthDatePicker.date = [NSDate date];
    //get the textfield provided in the plain text alertview
    birthDateInput = [setBirthDate textFieldAtIndex:0];
    //change the textfields inputView to the date picker
    birthDateInput.inputView = birthDatePicker;
    [birthDateInput setTextAlignment:NSTextAlignmentCenter];
    //create a date formatter to suitably show the date
    birthDateFormatter = [[NSDateFormatter alloc] init];
    [birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
    //set the current date into the textfield
    birthDateInput.text = [birthDateFormatter stringFromDate:[NSDate date]];
    //show the alert view and activate the textfield
    [setBirthDate show];
    [birthDateInput becomeFirstResponder];
    }
    

    那么别忘了在日期选择器中处理变化

    - (void) dateChanged:(id)sender
    {
    NSDateFormatter *birthDateFormatter;
    UIDatePicker *birthDatePicker = (UIDatePicker *)sender;
    
    birthDateFormatter = [[NSDateFormatter alloc] init];
    [birthDateFormatter setDateStyle:NSDateFormatterShortStyle];
    birthDateInput.text = [birthDateFormatter stringFromDate:birthDatePicker.date];
    }
    

    希望对你有帮助

    【讨论】:

    • 感谢您的帮助:)
    【解决方案2】:

    在 iOS 7 中你可以使用

    [alertView setValue:yourDataPicker forKey:@"accessoryView"];
    

    完整解释是here

    【讨论】:

      【解决方案3】:

      UIAlertView 的层次结构是私有的,不应修改。

      来自Apple docs

      UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

      在 iOS 6 之前,您可以将一些视图添加到 UIAlertView 子视图层次结构,但这在 iOS 7 上似乎不再适用。为 iOS7 宣布的 contentView 属性自 beta4 以来已成为私有 ivar释放,从而使addSubview 无法提醒视图。

      UIView *_contentViewNeue;
      

      有关更多信息,请参阅Apple dev forum discussion(您需要登录)

      在这种情况下,您应该创建一个模仿UIAlertView 的自定义视图,然后使用它来添加选择器视图。

      希望有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-04
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多