显示星期日的日期

显示星期日的日期

本文介绍了UIDatePicker 只显示星期日的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何设置 UIDatePicker 值以在 swift iOS 中仅显示星期日的日期?

How we can set UIDatePicker values to show only Sunday's date in swift iOS ?

推荐答案

最后,我自己找到了解决方案.在 didSelectRow 方法中检查所选日期是否为星期日???如果是,则可以,但如果不是,则重新加载组件以选择最近的星期日的日期.

finally, I myself found solution. In didSelectRow Method check if the selected day is sunday??? if yes then ok but if not then reload component to select date of nearest sunday.

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: 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, inComponent: 1, animated: true)
        }


        var confirmSunday = self.isGivenDaySunday(dayValue + 1, selectedMonth: monthValue, selectedYear: yearValue)
      //  Added by mohsin : Reason bug : selecting previous day
        if confirmSunday.sundayDate?.isLessThanDate(NSDate()) == true {

             confirmSunday = self.isGivenDaySunday(dayValue, selectedMonth: monthValue, 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, inComponent: 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, .Month, .Year, .Weekday]

func isGivenDaySunday(selectedDay: Int, selectedMonth: Int, selectedYear: Int) -> (isSunday: Bool, nextSundayAsWeekDay: Int, sundayDate: NSDate?) { let unitFlags: NSCalendarUnit = [.Day, .Month, .Year, .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, 0, selectedDate)
    } else {
       return (false, 8 - newSelectedDateComponent.weekday, nil)

    }

}

这篇关于UIDatePicker 只显示星期日的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:35