ios - 当一个应用程序打开时,如何让用户输入的两个警报一个接一个打开?

标签 ios swift uialertcontroller

我正在尝试弹出两个提醒,以便为一个应用输入两个玩家名称。我尝试在 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: {
})

您可以使用这行代码,例如在 viewDidLoadviewDidAppear 函数中首次出现的 UIViewController。

希望对你有帮助

关于ios - 当一个应用程序打开时,如何让用户输入的两个警报一个接一个打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632123/

相关文章:

ios - 测试 SwiftUI 正文 View 和属性

ios - Firebase 和 Swift 3 代码不再执行

ios - 使用 Reactive Cocoa swift 验证文本字段

ios - NSNotification:尝试在其 View 不在窗口层次结构中的 ViewController 上显示 UIAlertController

iOS UIAlertController 中断动画

swift - 尝试在 tvOS 上输入文本时,UISearchController 与 UIAlertController 结合出现问题

objective-c - 如何测试 NSUserDefaults 用户设置中是否存在 BOOL?

ios - iOS 开发者帐户中的 200 个测试设备

objective-c - 来自 Objective-C 协议(protocol)的 Swift 扩展

objective-c - 如何在 Objective C 中释放另一个对象拥有的对象