Uialtertview - >插入字符串 - >冥想怎么样?

时间:2015-02-14 08:14:17

标签: ios swift segue uialertview

我正在尝试执行此序列

  1. 点按按钮
  2. 显示带文本字段的UiAltertView
  3. 按OK
  4. 在另一个视图中复制通过键盘插入的文本
  5. 我的实际代码没有segue,我不明白为什么 当uialertview出现时,这就是我从输出中得到的结果:

    Game1[615:11540] <UIView: 0x798c1d20; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x798ca5f0>>'s window is not equal to <UIAlertController: 0x78e9b6b0>'s view's window!
    

    点按“确定”按钮后,视图无法更改

    以下是按钮的代码:

       @IBAction func saveScorePressed(sender: AnyObject) {
        let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: UIAlertControllerStyle.Alert)
            namePrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    
        namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
            textField.placeholder = "Name"
                    })
    
        presentViewController(namePrompt, animated: true, completion: nil)
        name = //how do i copy from uialtertview textfield?
        self.performSegueWithIdentifier("writelb", sender: nil)
    
    }
    

    这是segue:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if (segue.identifier=="writelb")
        {
            let destinationVC:ViewController4 = segue.destinationViewController as ViewController4
            destinationVC.score = score
            destinationVC.errors = errors
            destinationVC.combo = combo
            destinationVC.comboH = comboH
    
    
        }
    

    我认为segue代码是正确的,因为我在应用程序的其他部分使用没有任何问题 我不知道如何使序列工作以及如何将数据从Uialertviewtextfield复制到String变量

1 个答案:

答案 0 :(得分:0)

在调用presentViewController后,您无法在行中获取该名称,因为该调用不会阻止。它呈现视图控制器然后继续。您应该将segue的代码放入“确定”按钮的处理程序中。由于segue的发件人可以是任何对象,因此您可以从警报中获取textField并将其作为发件人。然后在prepareForSegue中获取文本并将其发送到目标视图控制器。

@IBAction func goPressed(sender: AnyObject) {
    let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: .Alert)

    namePrompt.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
        if let textFields = namePrompt.textFields as? [UITextField] {
            // Grab the first (only) text field and perform the segue designating
            // the textField as the sender.
            self.performSegueWithIdentifier("writelb", sender: textFields[0])
        }
    }))

    namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
        textField.placeholder = "Name"
    })

    presentViewController(namePrompt, animated: true, completion: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "writelb" {
        let dvc = segue.destinationViewController as ViewController4
        // Get name from textField and pass it to the destination view controller.
        dvc.name = (sender as UITextField).text
    }
}