How to block days in UIDatePicker for iOS

蓝咒 提交于 2019-12-13 16:31:16

问题


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?

Thanks.


回答1:


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 UIControlEventValueChanged as described here

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.




回答2:


Here is the Swift 4 answer to this question (original answer by 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)
    }
}



回答3:


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];
    }
}


来源:https://stackoverflow.com/questions/12206336/how-to-block-days-in-uidatepicker-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!