我目前正在尝试使用内部的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)

最佳答案

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)

关于iphone - 将TextField添加到AlertController会中断AlertController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47499359/

10-12 12:47