简体   繁体   中英

How do I use a date (selected from a date picker) to allow me to segue to a specific View Controller?

In my app the different View Controllers represent each month.

I want the date to lead me to the corresponding controller.

My goal is to create an app that presents the user their Zodiac when chosen their birthday from a date picker and clicked continue.

You can get month number and after that select the controller with switch help:

let monthNumber = 10

switch monthNumber {
case 1:
    *show some controller*
case 2:
    *show some controller*
case 3:
    *show some controller*
case 4:
    *show some controller*
case 5:
    *show some controller*
case 6:
    *show some controller*
case 7:
    *show some controller*
case 8:
    *show some controller*
case 9:
    *show some controller*
case 10:
    *show some controller*
case 11:
    *show some controller*
case 12:
    *show some controller*
default:
    break
}

But this is not good solution. Good is send date parameter to your Zodiac controller and configure it with this parameter.

You can possibly select the Date From Date Picker:

Step 1:- Extract Month component from it:

let dateComponent = Calendar.current.dateComponents([.month], from: selectedDateFromDatePicker)
print(dateComponent.month!)

Step:-2 Push to specific View Controller According to Month:

    if dateComponent.month! == 1{
        //January
    }else if dateComponent.month! == 2{
        //Feb
    }else if dateComponent.month! == 3{
        //Mar
    }else if dateComponent.month! == 4{
        //Apr
    }else if dateComponent.month! == 5{
        //May
    }else if dateComponent.month! == 6{
        //June
    }else if dateComponent.month! == 7{
        //July
    }else if dateComponent.month! == 8{
        //Aug
    }else if dateComponent.month! == 9{
        //Sep
    }else if dateComponent.month! == 10{
        //Oct
    }else if dateComponent.month! == 11{
        //Nov
    }else{
        //Dec
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM