如何在选择日期后禁用UIDatePicker?
我正在为 UIDatePicker 使用以下代码,无论我选择的日期是什么,它都会显示在标签中。但我需要在选择日期后关闭选择器。这可能吗请建议我。
我正在使用以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:label];
[label release];
// Initialization code
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)changeDateInLabel:(id)sender{
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
[df release];
}
Iam using the following code for the UIDatePicker, whatever the date iam selecting it is being displayed in the label. But i need to close the Picker after selecting the date. Is this possible please suggest me.
I am using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:label];
[label release];
// Initialization code
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)changeDateInLabel:(id)sender{
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
[df release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议您执行以下操作:
使用带有“完成”按钮和“取消”按钮的工具栏
...
这只是一个快速实施,它可能会有所帮助
I recommend you to do something like this:
to use a toolbar with a Done button and maybe a Cancel Button
...
This is just a quick implementation, it may help
您必须使用按钮单击背景才能从您的视图中隐藏 DATEPICKER。
You Have to use button for click on Background to hide the DATEPICKER from your view.
也许比从超级视图中删除更好的是将 userInteraction 设置为 NO
并且您需要“完成”按钮,就像用户准备好完成选择日期的地方一样
Maybe better then removing from superview would be setting userInteraction to NO
and you need button for "done", as in where the user is ready to finish chosing date
以下代码将起作用。
点击日期标签将启用 DatePicker,如下所示:
使用以下方法,点击 Picker 外部时,Picker 将从视图中退出。
Following code will work.
Tapping on the date label will enable the DatePicker as follows:
and using the following method Picker will be resigned from the view on tapping outside the Picker.