转到下一个 ViewController 并在条件有效时传递数据
segue to next ViewController and pass data if condition is valid
我正在尝试从第一个 viewcontroller 转到第二个,并将 var dateAndTime = NSDate()
的值传递给第二个视图控制器。
我的第一种方法是从第一个 viewcontroller 本身转场到第二个,在属性检查器中定义转场的名称。
接下来,我在第一个viewcontroller中link一个按钮@IBAction func nextButtonToFifthViewController
,使用了一个if else
语句和prepareForSegue
,但是代码编译不通过,因为我不允许覆盖 prepareForSegue
.
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a time betwen 7:00 - 19:00"
} else {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destViewController:FifthViewController = segue.destinationViewController as! FifthViewController
destViewController.dateAndTimeSelected = dateAndTime }
}
我的第二种方法是使用 performSegueWithIdentifier
,但我无法将数据传递到下一个视图控制器,因为 dateAndTimeSelected
在第一个视图控制器中不可用。
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a time betwen 7:00 - 19:00"
} else {
performSegueWithIdentifier("toFifthViewController", sender: self)
dateAndTimeSelected = dateAndTime
}
注意performSegueWithIdentifier
和prepareForSegue
方法应该一起使用。当您确定需要移动到新的视图控制器时,首先调用 performSegueWithIdentifier
,然后您可以在 prepareForSegue
中配置新的视图控制器
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a 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
}
}
}
我正在尝试从第一个 viewcontroller 转到第二个,并将 var dateAndTime = NSDate()
的值传递给第二个视图控制器。
我的第一种方法是从第一个 viewcontroller 本身转场到第二个,在属性检查器中定义转场的名称。
接下来,我在第一个viewcontroller中link一个按钮@IBAction func nextButtonToFifthViewController
,使用了一个if else
语句和prepareForSegue
,但是代码编译不通过,因为我不允许覆盖 prepareForSegue
.
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a time betwen 7:00 - 19:00"
} else {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destViewController:FifthViewController = segue.destinationViewController as! FifthViewController
destViewController.dateAndTimeSelected = dateAndTime }
}
我的第二种方法是使用 performSegueWithIdentifier
,但我无法将数据传递到下一个视图控制器,因为 dateAndTimeSelected
在第一个视图控制器中不可用。
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a time betwen 7:00 - 19:00"
} else {
performSegueWithIdentifier("toFifthViewController", sender: self)
dateAndTimeSelected = dateAndTime
}
注意performSegueWithIdentifier
和prepareForSegue
方法应该一起使用。当您确定需要移动到新的视图控制器时,首先调用 performSegueWithIdentifier
,然后您可以在 prepareForSegue
@IBAction func nextButtonToFifthViewController(sender: AnyObject) {
if timeRestricted.contains(hourComponents){
self.label.text = "Please choose a 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
}
}
}