【发布时间】:2015-03-06 14:18:02
【问题描述】:
我需要停止转场并使用textField 显示警报。
但是,当我按下“确定”按钮时,我想传递到下一个视图,textfield 的内容。
问题是当我在prepareForSegue 中声明一个视图控制器目标并传递文本时,因为这是零。我写我的例子:
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
var shouldSegue:Bool = true
if identifier == "FirstToSecondView" {
var alert = UIAlertController(title: "End the First", message: "Are you sure you want to end the first?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler{ (textField) in
textField.placeHolder = "Name"
}
presentViewController(alert, animated: true, completion: nil)
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
println("OK Pressed")
let tf = alert.textFields?.First as? UITextField
//name is a declared variable above
if tf != nil {self.name = tf.text}
self.performSegueWithIdentifier("WorkoutToSummary", sender: self)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
println("Cancel Pressed")
shouldSegue = false
}
alert.addAction(okAction)
alert.addAction(cancelAction)
}
return shouldSegue
}
然后在prepareForSegue 中,我输入:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
var namePass: String = self.name
if segue.identifier == "FirstToSecondView" {
let secondViewController = segue.destinationViewController as SecondViewController
secondViewController.name = namePass
}
}
但在prepareForSegue 启动的那一刻,self.name 为nil
提前致谢!!
【问题讨论】:
-
明确表示您希望当您单击确定时,警报视图 textfeild 中的文本应该传递其他控制器变量??对
-
是的,就是这样。谢谢!!
-
我已经在下面写了我的答案,希望它能正常工作,我也在使用相同的方法