繁体   English   中英

在“完成”按钮上按下UIDatePicker获取.date属性

[英]UIDatePicker get .date property on “done” button press

我有一个自定义视图,其顶部带有UIDatePicker和完成按钮。

当我按下完成按钮时,我想获取UIDatePicker中时间

当我按下按钮时会收到错误消息(由于发件人是UIBarButtonItem,所以我期望如此),并且更改日期时间可以按预期工作。

其他解决方案显示了单独的选择器,但是,我希望(如果可能)调用相同的选择器,并像在选择器视图中滚动一样获取选定的选择器。

我考虑过更改选择器方法以将其作为id接收,但这没有用。

这是我目前正在做的事情,我觉得这可能很愚蠢:

UIView *v = [[UIView alloc] init];

// Create the date time picker
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView = [[UIDatePicker alloc] initWithFrame:pickerFrame];

pView.datePickerMode = UIDatePickerModeTime;
[pView addTarget:self action:@selector(dateTimePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

dateTimePicker = pView;
[v addSubview:dateTimePicker];

// done button
CGRect toolBarFrame = CGRectMake(0, 0, 320, 44);
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dateTimePickerValueChanged:)];
NSArray *toolBarItems = [NSArray arrayWithObjects:flexibleItem, doneButton, nil];
[toolBar setItems:toolBarItems];
[v addSubview:toolBar];

- (void)dateTimePickerValueChanged:(UIDatePicker *)datePicker {
    NSLog(@"time on change: %@", datePicker.date);

图片参考:

在此处输入图片说明

您是对造成此崩溃的原因的正确说法。 我认为分离选择器是正确的道路。 您可以让两个选择器都调用相同的方法。 这样,您就没有任何复制的代码。

编写不带参数的方法,并使UIDatePicker成为整个类的属性或全局对象,以便您可以从任何位置访问它。

现在以这种方式将您的方法传递给两个选择器,您将在完成按钮按下时获得日期。

- (void)dateTimePickerValueChanged {
    NSLog(@"time on change: %@", datePicker.date);
}

正确的方法是创建一个NSDate对象,该对象可根据您的要求进行访问。 现在,从UIDatePicker ,创建一个更改值的方法。 每当调用此方法时,请将date .date属性的值设置为NSDate对象。 按下“完成”按钮时,使用NSDate对象的值。 它将为您提供正确的价值。

@interface ViewController(){
NSDate *globalDate;
}
@end

@implementation ViewController 

- (void)dateTimePickerValueChanged {
    globalDate = datePicker.date;
}

- (IBAction)donePressed {
NSLog(@"%@",globalDate);
}

@end

时间

 datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeTime;
[yourField setInputView:datePicker];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];


UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];



[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[yourField setInputAccessoryView:toolBar];



-(void)ShowSelectedDate
{
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
dateFormat.dateStyle=NSDateFormatterMediumStyle;
[dateFormat setDateFormat:@"hh:mm"];
NSString *str=[NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:datePicker.date]];
//assign text to label
yourField =str;
[yourField resignFirstResponder];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM