如何从 UIAlertController 中调用文本字段?
How do I call a textfield from within an UIAlertController?
所以,我正在制作一个应用程序,当按下 "Start" 按钮时,会弹出一个通过 UIAlertController
的警报,并且有两个文本字段询问玩家的名字,我将移动到哪里通过
将玩家名称显示在游戏屏幕上(这是一个井字游戏应用程序)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
所以我现在的问题是如何将用户输入 TextFieldWithConfigurationHandler
的值获取到我的 GameScreen。我的 UIAlertController
代码如下:
@IBAction func startButton(sender: AnyObject)
{
var playerNameEntry = UIAlertController(title: "Player names", message: nil, preferredStyle: .Alert)
playerNameEntry.addTextFieldWithConfigurationHandler({textfield in textfield.placeholder = "Player 1"})
playerNameEntry.addTextFieldWithConfigurationHandler({textfield in textfield.placeholder = "Player 2"})
var continueButton = UIAlertAction(title: "Continue", style: .Default, { action in
self.performSegueWithIdentifier("gameSegue", sender: nil)
})
playerNameEntry.addAction(continueButton)
var cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
playerNameEntry.addAction(cancelButton)
self.presentViewController(playerNameEntry, animated: true, completion: nil)
}
感谢您花时间阅读本文,如果您回答,十分感谢
在您的 continueButton
处理程序中,您可以作为 playerNameEntry
访问警报控制器。因此,您可以访问其 textFields
数组:
因此您可以访问每个文本字段的 text
。
var continueButton = UIAlertAction(title: "Continue", style: .Default, { action in
// code goes here
let tfs = playerNameEntry.textFields as [UITextField]
// ...do stuff with the `text` of each text field...
self.performSegueWithIdentifier("gameSegue", sender: nil)
})
所以,我正在制作一个应用程序,当按下 "Start" 按钮时,会弹出一个通过 UIAlertController
的警报,并且有两个文本字段询问玩家的名字,我将移动到哪里通过
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
所以我现在的问题是如何将用户输入 TextFieldWithConfigurationHandler
的值获取到我的 GameScreen。我的 UIAlertController
代码如下:
@IBAction func startButton(sender: AnyObject)
{
var playerNameEntry = UIAlertController(title: "Player names", message: nil, preferredStyle: .Alert)
playerNameEntry.addTextFieldWithConfigurationHandler({textfield in textfield.placeholder = "Player 1"})
playerNameEntry.addTextFieldWithConfigurationHandler({textfield in textfield.placeholder = "Player 2"})
var continueButton = UIAlertAction(title: "Continue", style: .Default, { action in
self.performSegueWithIdentifier("gameSegue", sender: nil)
})
playerNameEntry.addAction(continueButton)
var cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
playerNameEntry.addAction(cancelButton)
self.presentViewController(playerNameEntry, animated: true, completion: nil)
}
感谢您花时间阅读本文,如果您回答,十分感谢
在您的 continueButton
处理程序中,您可以作为 playerNameEntry
访问警报控制器。因此,您可以访问其 textFields
数组:
因此您可以访问每个文本字段的 text
。
var continueButton = UIAlertAction(title: "Continue", style: .Default, { action in
// code goes here
let tfs = playerNameEntry.textFields as [UITextField]
// ...do stuff with the `text` of each text field...
self.performSegueWithIdentifier("gameSegue", sender: nil)
})