func pickerVIEw(pickerVIEw:UIPickerVIEw,dIDSelectRow行:Int,inComponent组件:Int){
if component == 0 { pickerVIEw.reloadComponent(1) } let TitleLabel = pickerVIEw.vIEwForRow(row,forComponent: component) as? UILabel TitleLabel?.Font = UIFont(name: BCGConstants.Fonts.name.ProximaNovaBold,size: 27)! var dayValue = pickerVIEw.selectedRowInComponent(1) + 1 let monthValue = pickerVIEw.selectedRowInComponent(0) + 1 var yearValue = 0 let unitFlags: NSCalendarUnit = [.Day,.Month,.Year,.Weekday] let currentDateComponents = NSCalendar.currentCalendar().components(unitFlags,fromDate: NSDate()) if monthValue > currentDateComponents.month || (dayValue >= currentDateComponents.day && monthValue == currentDateComponents.month ) { yearValue = currentDateComponents.year } else { yearValue = currentDateComponents.year + 1 } deBUGPrint("\(self.isgivendaySunday(dayValue,selectedMonth: monthValue,selectedYear: yearValue)) day = \(dayValue) month = \(monthValue) )") let sundayCheck = self.isgivendaySunday(pickerVIEw.selectedRowInComponent(1) + 1,selectedMonth: pickerVIEw.selectedRowInComponent(0) + 1,selectedYear: yearValue) if sundayCheck.isSunday { self.startDateTextFIEld.text = sundayCheck.sundayDate?.fullStyleDateString self.newBootcamp?.startDate = sundayCheck.sundayDate! } else { // TitleLabel?.Font = UIFont(name: BCGConstants.Fonts.name.ProximaNovaBold,size: 27)! // TitleLabel?.textcolor = UIcolor.lightGraycolor() if dayValue > 15 { dayValue = pickerVIEw.selectedRowInComponent(1) - (7 - sundayCheck.nextSundayAsWeekDay) pickerVIEw.selectRow(dayValue,inComponent: 1,animated: true) } else { dayValue = pickerVIEw.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay pickerVIEw.selectRow(dayValue,animated: true) } var confirmSunday = self.isgivendaySunday(dayValue + 1,selectedYear: yearValue) // Added by mohsin : Reason BUG : selecting prevIoUs day if confirmSunday.sundayDate?.isLessthanDate(NSDate()) == true { confirmSunday = self.isgivendaySunday(dayValue,selectedYear: yearValue + 1) //Todo: Need to be verify again : If not working fine then you must try to change next commented statement and uncomment it // dayValue = pickerVIEw.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay pickerVIEw.selectRow(dayValue - 1,animated: true) } self.startDateTextFIEld.text = confirmSunday.sundayDate?.fullStyleDateString self.newBootcamp?.startDate = confirmSunday.sundayDate! deBUGPrint(confirmSunday.sundayDate?.fullStyleDateString) }}
检查星期日的方法是跟随一个
func isgivendaySunday(selectedDay:Int,selectedMonth:Int,selectedYear:Int) – > (isSunday:Bool,nextSundayAsWeekDay:Int,sundayDate:NSDate?){
let unitFlags:NSCalendarUnit = [.Day,.Weekday]
let selectedDateComponents = NSDateComponents() selectedDateComponents.month = selectedMonth selectedDateComponents.day = selectedDay selectedDateComponents.year = selectedYear let selectedDate = NSCalendar(IDentifIEr: NSCalendarIDentifIErGregorian)?.dateFromComponents(selectedDateComponents) let newSelectedDateComponent = NSCalendar.currentCalendar().components(unitFlags,fromDate: selectedDate!) if newSelectedDateComponent.weekday == 1 { // 1 means SUNDAY as per Gregorian return (true,selectedDate) } else { return (false,8 - newSelectedDateComponent.weekday,nil) }}总结
以上是内存溢出为你收集整理的swift – UIDatePicker仅显示星期日的日期?全部内容,希望文章能够帮你解决swift – UIDatePicker仅显示星期日的日期?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)