2016-01-15 87 views
0

我有一个日期选择器,我想限制日期选择器显示日期和日期以及月份,但该月份不能更改,它会附加到日期。即1月15日星期五。1月15日是一个组成部分。它应该显示当天加上接下来的6天,所以一周。我不想要这一年。如何仅在Datepicker上允许日期和日期?

let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! 
    let currentDate: NSDate = NSDate() 
    let components: NSDateComponents = NSDateComponents() 
    let minDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))! 
    components.year = +7 
    let maxDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))! 
    self.datepicker.minimumDate = minDate 
    self.datepicker.maximumDate = maxDate 

enter image description here

+4

你不能使用'UIDatePicker'。你需要使用'UIPickerView'。 – rmaddy

+0

这很不幸,谢谢! – coolmac

回答

0

首先,你必须使用两节UIPickerView和并实现数据源与两个月份和日期部分。

但实际上并不像看起来那么容易。至少因为你不知道今年2月有多少天,因为你不选择一年。

+0

http://stackoverflow.com/a/29956607/919545 –

+1

@AnkitSrivastava这个解决方案中的关键词是'forDate:date',其中date包含当前日期以及一年。但提问者没有指定任何一年。所以2月份的问题仍然存在。 –

0

请原谅我的无知,如果我在这里基地的方式,但不能在这里使用NSDateFormatter?

let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "MMMM" 
convertedDate = dateFormatter.stringFromDate(currentDate) 
相关问题