xcode 如何在 iOS 的 UIDatePicker 中阻止天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12206336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:22:58  来源:igfitidea点击:

How to block days in UIDatePicker for iOS

iphoneiosxcodedatepickeruipickerview

提问by Pach

I'm using a Date picker on my IOS app, and I want to know if is possible to block some days. For example, I need to block all mondays from a year, is this possible?

我在我的 IOS 应用程序上使用日期选择器,我想知道是否可以阻止几天。例如,我需要阻止一年中的所有星期一,这可能吗?

Thanks.

谢谢。

回答by E. Lüders

The only thing you can do is add a custom UIPickerView as described in the comments or implement a method that is called for the event UIControlEventValueChangedas described here

你唯一可以做的事情是在评论描述添加自定义UIPickerView或实现一个被称为该事件的方法UIControlEventValueChanged描述在这里

then check the new value for a valid weekday. you can get the weekday with:

然后检查有效工作日的新值。您可以通过以下方式获得工作日:

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
return [components weekday]; // 1 = Sunday, 2 = Monday, ...

There is no way to hide some days in the scrollwheel.

无法在滚轮中隐藏某些日子。

回答by iCappsCVA

Here is the Swift 4answer to this question (original answer by Nicholas Harlen):

这是这个问题的Swift 4答案(Nicholas Harlen 的原始答案):

let datePicker: UIDatePicker = UIDatePicker(frame: .zero)
    datePicker.datePickerMode = .date
    datePicker.addTarget(self, action: #selector(datePickerValueChanged(_:)), for: .valueChanged)

@objc func datePickerValueChanged(_ sender: UIDatePicker) {
    let calender: Calendar = Calendar(identifier: .gregorian)
    let weekday = calender.component(.weekday, from: sender.date)
    //sunday
    if weekday == 1 {
        datePicker.setDate(Date(timeInterval: 60*60*24*1, since: sender.date), animated: true)
    } else if weekday == 7 {
        datePicker.setDate(Date(timeInterval: 60*60*24*(-1), since: sender.date), animated: true)
    }
}

回答by Nicholas Harlen

I had to do something similar and couldn't find a way of removing dates. However what I ended up doing was if an invalid date is selected it would bounce away to the nearest day that was valid, similar to how the control handles if you set a minimum date and try and choose a date before that.

我不得不做类似的事情,但找不到删除日期的方法。但是,我最终做的是,如果选择了无效日期,它会反弹到最近的有效日期,类似于如果您设置最小日期并尝试选择之前的日期,控件的处理方式。

-(void)init
{
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    [datePicker setMinimumDate: [NSDate date]];
    [datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
}

-(void)onDatePickerValueChanged:(UIDatePicker *)picker
{
    // Work out which day of the week is currently selected.
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:picker.date];
    int weekday = [comps weekday];

    // Bounce Sundays (weekday=1) forward a day
    if (weekday == 1)
    {
        [datePicker setDate:[NSDate dateWithTimeInterval:60*60*24*1 sinceDate:picker.date] animated: true];
    }
    // Bounce Saturdays (weekday=7) back a day
    else if (weekday == 7)
    {
        [datePicker setDate:[NSDate dateWithTimeInterval:60*60*24*-1 sinceDate:picker.date] animated: true];
    }
}