我正在尝试弹出两个提醒,以便为一个应用输入两个玩家名称。我尝试在 viewDidLoad 和 viewDidAppear 函数中使用 AlertController 代码。虽然其中一个在 viewDidAppear 中运行良好,但在调用第二个时会抛出错误,因为它会继续运行其他代码。
理想情况下,我希望它弹出并说“输入玩家 1 名称”,让用户有机会输入名称,然后在按下提交时开始执行第二个警报,以便弹出“输入玩家 2 姓名”。
最佳答案
您需要为 UIAlertAction 实现一个处理程序,在其中显示另一个警报。看一下这段代码:
let firstAlert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
firstAlert.addTextFieldWithConfigurationHandler({
textField in
textField.placeholder = "Some input"
})
let secondAlert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
secondAlert.addTextFieldWithConfigurationHandler({
textField in
textField.placeholder = "Some input 2"
})
firstAlert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: {
action in
print("text from first alert : + \(firstAlert.textFields?[0].text)")
self.presentViewController(secondAlert, animated :true, completion :nil)
}))
secondAlert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: {
action in
print("text from second alert : + \(firstAlert.textFields?[0].text)")
}))
self.presentViewController(firstAlert, animated: true, completion: {
})
您可以使用这行代码,例如在 viewDidLoad
或 viewDidAppear
函数中首次出现的 UIViewController。
希望对你有帮助
关于ios - 当一个应用程序打开时,如何让用户输入的两个警报一个接一个打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632123/