【问题标题】:Stop Segue and pass to next view the content of textfield shown in alert - Swift停止 Segue 并将警报中显示的文本字段的内容传递到下一个视图 - Swift
【发布时间】: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 中的文本应该传递其他控制器变量??对
  • 是的,就是这样。谢谢!!
  • 我已经在下面写了我的答案,希望它能正常工作,我也在使用相同的方法

标签: ios iphone swift segue


【解决方案1】:

首先你要做的是为你的警报设置一个标签为

alert.tag = 1

在你的视图控制器中创建一个变量

class SBViewController : UIViewController{
//GlobalVariables
var demoText : String = String()

override func viewDidLoad(){
super.viewDidLoad()
}
}

为您的警报视图编写一个委托方法并将您的警报视图委托设置为 self

alert.delegate = self
var name : String = String()



  func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

            if alertView.tag == 20 {
                if alertView.buttonTitleAtIndex(buttonIndex) == "OK"{
                    var textFeild : UITextField = alertView.textFieldAtIndex(0)!
                    if textFeild.text != "" {
                    name =  textFeild.text


                  }

                }
            }

【讨论】:

  • 为什么要写一个 alert.tag?我已经使用 segue push 从按钮链接了两个视图。我只需要停止 segue 转换并使用文本字段显示警报以捕获将 prepareForSegue 发送到 secondView 的勇气。
  • 就这么简单,单独写segue的代码,把alert的textfeild文本赋值给一个全局变量,然后使用segue函数
  • 那是我已经实现了,但是不行,因为当我在prepareForSegue中做“var namePass: String = self.name”时,self.name还是nil
猜你喜欢
  • 2012-03-13
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多