iOS Datepicker:如何锁定无效日期? [英] iOS Datepicker: How to LOCK invalid dates?
问题描述
最近,我一直在争取最大/最小日期,老实说,我不知道我在这里缺少什么.我进行了很多搜索,似乎没有人对此抱怨. (我不是在谈论设置最大/最小日期,因为它们只能使无效的日期显示为灰色)
I've been struggling with the maximum/minimum date lately, and honestly, I don't know what I'm missing here. I searched a lot and no one seems to complain about it. (I'm not talking about setting a maximum/minimum date as these can only make invalid dates appear gray)
这是我到目前为止所做的:
Here's what I've done so far:
[myDatePicker addTarget:self action:@selector(disableDate) forControlEvents:UIControlEventValueChanged];
然后,在disableDate上,我有这样的内容:
Then, on disableDate, I have something like this:
if ([myDatePicker.date compare:[NSDate date]] == NSOrderedDescending) {
MyDatePicker.date = [NSDate date];
}
(或升序,这取决于我要禁用的日期)
(Or ascending, it depends on which dates I want to disable)
问题是:它可以正常运行,但只能运行一次.换句话说:打开视图并尝试选择一个视图(例如2007年)后,它就会滚动回到2013年.太好了.但是在那之后,如果我尝试滚动到任何无效的日期(包括2007年),它将不再向后滚动,直到我重新打开视图. 我觉得这里缺少一些简单的东西,但是我不知道是什么.
The thing is: it works fine but only ONCE. In other words: as soon as I open the view and try to pick, for example, 2007, it scrolls back to 2013. Great. But right after that, if I try to scroll to any invalid date (including 2007), it won't scroll back anymore until I reopen my view. I feel like I'm missing something simple here, but I don't know what.
非常感谢!
PS:只需提一下,我已将其放入disableDate
PS: Just to mention, I've put this inside disableDate
NSLog(@"Value changed");
只是为了确保每次我滚动日期选择器时都调用该方法,是的,它是有效的;
just to make sure it's calling the method everytime I scroll the datepicker, and yes, it is working;
PS2:我尝试过相同的操作,但使用的是
PS2: I've tried the same but using
- (IBAction)datePickerValueChanged:(id) sender
相反,它的行为完全相同.
instead and it had exactly the same behaviour.
推荐答案
因此,为了重置" UIDatePicker
,您可以使用reloadInputViews
方法(因为UIDatePicker
是UIResponder
So in order to "reset" the UIDatePicker
you can use reloadInputViews
method (since UIDatePicker
is subclass of UIResponder
拼写
这篇关于iOS Datepicker:如何锁定无效日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!