将TextField添加到AlertController会中断AlertController

时间:2017-11-26 18:07:43

标签: iphone swift alert textfield uialertcontroller

我目前正在尝试打开一个带有TextField的AlertController。运行时

let configAlert = UIAlertController(title: "Configure Add-On", message: "Enter Your Add-On Name:", preferredStyle: UIAlertControllerStyle.alert)

configAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
      // Handle Input          
}))

present(configAlert, animated: true, completion: nil)

一切正常,但只要我添加TextField

configAlert.addTextField { (textField) in
    textField.placeholder = "Name"
}

警报需要大约10倍的时间打开,立即解散,我在控制台中发现此错误大约30次:

2017-11-26 13:04:08.985783-0500 MinelyMod[380:14792] Warning: Attempt to dismiss from view controller <UISplitViewController: 0x147e0a6a0> while a presentation or dismiss is in progress!

这是完成的AlertController失败

let configAlert = UIAlertController(title: "Configure Add-On", message: "Enter Your Add-On Name:", preferredStyle: UIAlertControllerStyle.alert)

configAlert.addTextField { (textField) in
    textField.placeholder = "Name"
}

configAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
    // Handle Input            
}))

present(configAlert, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:0)

let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
        alert -> Void in
        let textField = alertController.textFields![0] as UITextField
        // do something with textField
    }))
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
        textField.placeholder = "Search"
    })

    self.present(alertController, animated: true, completion: nil)