Ios 无法从UIDatePicker获取日期,UIDatePicker在单击TextField时打开的UIActionSheet内部使用

Ios 无法从UIDatePicker获取日期,UIDatePicker在单击TextField时打开的UIActionSheet内部使用,ios,datepicker,uiactionsheet,Ios,Datepicker,Uiactionsheet,调用acton表的方法 - (void)textFieldDidBeginEditing:(UITextField *)textField { [txtSourceTime resignFirstResponder]; UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveB

调用acton表的方法

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [txtSourceTime resignFirstResponder];

    UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    [sheet1 setActionSheetStyle:UIActionSheetStyleDefault];
    [sheet1 setTag:3];
    [sheet1 showInView:self.view];

    time = [[UIDatePicker alloc] init];
    time.datePickerMode = UIDatePickerModeDateAndTime;
    time.timeZone = [NSTimeZone localTimeZone];

    [time setBounds:CGRectMake(0, -125, 320, 200)];
    [sheet1 addSubview:time];
    [sheet1 showFromRect:CGRectMake(0, 300, 320, 300) inView:self.view animated:YES];
    [sheet1 setBounds:CGRectMake(0, 0, 320, 500)];
}
在“操作表”中单击“完成”时,无法在文本框中获取日期添加以下代码

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        printf("\n cancel");

    }else{
      printf("\n Done");
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
        NSString *strDate = [dateFormatter stringFromDate:time.date];
        _txtSourceTime.text = strDate;

    }
}

需要添加一个
UIActionSheetDelegate
方法,以了解何时点击完成按钮,然后使用
UIDatePicker
中的日期更新
UIExtField
。例如:

在.h中:

添加
UIActionSheetDelegate
协议:

@interface MyClass : UIViewController <UIActionSheetDelegate, UITextFieldDelegate>
    // ...
@end
并为
UIActionSheet
设置代理:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //...

    UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    sheet1.delegate = self;

    //...
}
请注意,根据苹果的文档,这不是建议使用的
UIActionSheet

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //...

    UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    sheet1.delegate = self;

    //...
}
UIActionSheet不是设计为子类的,也不应该添加 视图到其层次结构


另一种选择是在
UITextField
上添加
UIDatePicker
作为
inputView
。对于“取消”和“完成”按钮,在
UITextField
上添加一个
inputAccessoryView
(该视图将显示在
UIDatePicker

的顶部,它在我的情况下运行良好,但它提供了格式为[dd/MM/YYYY hh:MM:ss TZD]的日期和时间,而我只需要[hh:MM]本地时区的设置是,使用
NSDateFormatter
获取所需的格式-来自@ssmanohar的答案包括一个示例。只需将
setDateFormat
更改为
@“HH:MM”
。如果只希望
UIDatePicker
显示时间,请将
datePickerMode
设置为
UIDatePickerModeTime
,例如
time.datePickerMode=UIDatePickerModeTime