使用 UIDatePicker 实时更新 UILabel
我已经把一切都安排好了。我只需要知道如何在用户浏览 UIDatePicker 时更新 UILabel。
NSDate *myDate =dateView.date;
dateFormatter = [[NSDateFormatter alloc] init];
//NSDate *date = [NSDate date];
[dateFormatter setDateFormat:@"MMM d y"];
NSString *dateString = [dateFormatter stringFromDate:myDate];
NSLog(@"New Date is = %@",dateString);
self.firstDateDisplayed.text = [self.dateFormatter stringFromDate:self.dateView.date];
}
(这一切都在 IBAction 中) 如果您需要更多代码,请告诉我。非常感谢。
I have everything set up. I just need to know how to get the UILabel to update as the user goes through the UIDatePicker.
NSDate *myDate =dateView.date;
dateFormatter = [[NSDateFormatter alloc] init];
//NSDate *date = [NSDate date];
[dateFormatter setDateFormat:@"MMM d y"];
NSString *dateString = [dateFormatter stringFromDate:myDate];
NSLog(@"New Date is = %@",dateString);
self.firstDateDisplayed.text = [self.dateFormatter stringFromDate:self.dateView.date];
}
(This is all in an IBAction)
If you need anymore code just let me know. Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
viewDidLoad
方法中添加类似的内容:然后是实际的目标方法。
或者在 IB 中,您可以将日期选择器的
valueChanged
事件连接到您的IBAction
方法。像所有
UIControl
一样,UIDatePicker
有一个名为UIControlEvent..
的范例,您在这里感兴趣的是valueChanged
;这与按钮事件触发UIControlEventTouchUpInside
的IBAction
方法类似。addTarget:action:forControlEvents:
所做的只是表明当该控件事件发生时您需要调用。You would put something like this in your
viewDidLoad
method:And then the actual target method.
Or in IB you can connect the datepicker's
valueChanged
event to yourIBAction
method.UIDatePicker
like allUIControl
s has a paradigm calledUIControlEvent..
the one your interested in here isvalueChanged
; It's similar to how button events fire yourIBAction
methods forUIControlEventTouchUpInside
. All thataddTarget:action:forControlEvents:
does is say that when that control event happens you want a call.