swift - segue to next ViewController and pass data if condition is valid -


i trying segue first viewcontroller second 1 , pass value of var dateandtime = nsdate() second view controller.

my first approach segue first viewcontroller second one, define name of segue in attributes inspector. next, link button @ibaction func nextbuttontofifthviewcontroller in first viewcontroller, use if else statement , prepareforsegue, code not compile because not allowed override prepareforsegue.

@ibaction func nextbuttontofifthviewcontroller(sender: anyobject) {     if timerestricted.contains(hourcomponents){         self.label.text = "please choose time betwen 7:00 - 19:00"     } else {      override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         var destviewcontroller:fifthviewcontroller = segue.destinationviewcontroller as! fifthviewcontroller         destviewcontroller.dateandtimeselected = dateandtime    }        } 

my second approach use performseguewithidentifier, wont able pass data next view controller because dateandtimeselected not available use in first view controller.

@ibaction func nextbuttontofifthviewcontroller(sender: anyobject) {     if timerestricted.contains(hourcomponents){         self.label.text = "please choose time betwen 7:00 - 19:00"     } else {          performseguewithidentifier("tofifthviewcontroller", sender: self)   dateandtimeselected = dateandtime } 

note performseguewithidentifier , prepareforsegue methods should used together. first call performseguewithidentifier when sure need move new view controller, may configure new view controller in prepareforsegue

@ibaction func nextbuttontofifthviewcontroller(sender: anyobject) {     if timerestricted.contains(hourcomponents){         self.label.text = "please choose time betwen 7:00 - 19:00"     } else {         performseguewithidentifier("tofifthviewcontroller", sender: self)     } }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if segue.identifier == "tofifthviewcontroller" {          if let destviewcontroller = segue.destinationviewcontroller as? fifthviewcontroller {             destviewcontroller.dateandtimeselected = dateandtime         }       } } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -