Uialtertview -> Insert String -> Segue 怎么样?
Uialtertview -> Insert String -> Segue How?
我正在尝试执行此序列
- 点击按钮
- 显示带有文本字段的 UiAltertView
- 按确定
- 在另一个视图中复制通过键盘插入的文本
我的实际代码没有segue,我不明白为什么
这是 uialertview 出现时我从输出中得到的:
Game1[615:11540] <UIView: 0x798c1d20; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x798ca5f0>>'s window is not equal to <UIAlertController: 0x78e9b6b0>'s view's window!
在我点击确定按钮后,视图没有改变
这是按钮的代码:
@IBAction func saveScorePressed(sender: AnyObject) {
let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: UIAlertControllerStyle.Alert)
namePrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Name"
})
presentViewController(namePrompt, animated: true, completion: nil)
name = //how do i copy from uialtertview textfield?
self.performSegueWithIdentifier("writelb", sender: nil)
}
这是转场:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier=="writelb")
{
let destinationVC:ViewController4 = segue.destinationViewController as ViewController4
destinationVC.score = score
destinationVC.errors = errors
destinationVC.combo = combo
destinationVC.comboH = comboH
}
我认为segue代码是正确的,因为我在应用程序的其他部分使用没有任何问题
我不知道如何使序列工作以及如何将数据从 Uialertviewtextfield 复制到字符串变量
您无法在调用 presentViewController
之后的行中获取名称,因为该调用不会阻塞。它呈现视图控制器然后继续。您应该将 segue 的代码放入 "OK" 按钮的处理程序中。由于 segue 的发送者可以是任何对象,您可以从警报中获取 textField 并将其设为发送者。然后在 prepareForSegue
中获取文本并将其发送到目标视图控制器。
@IBAction func goPressed(sender: AnyObject) {
let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: .Alert)
namePrompt.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
if let textFields = namePrompt.textFields as? [UITextField] {
// Grab the first (only) text field and perform the segue designating
// the textField as the sender.
self.performSegueWithIdentifier("writelb", sender: textFields[0])
}
}))
namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Name"
})
presentViewController(namePrompt, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "writelb" {
let dvc = segue.destinationViewController as ViewController4
// Get name from textField and pass it to the destination view controller.
dvc.name = (sender as UITextField).text
}
}
我正在尝试执行此序列
- 点击按钮
- 显示带有文本字段的 UiAltertView
- 按确定
- 在另一个视图中复制通过键盘插入的文本
我的实际代码没有segue,我不明白为什么 这是 uialertview 出现时我从输出中得到的:
Game1[615:11540] <UIView: 0x798c1d20; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x798ca5f0>>'s window is not equal to <UIAlertController: 0x78e9b6b0>'s view's window!
在我点击确定按钮后,视图没有改变
这是按钮的代码:
@IBAction func saveScorePressed(sender: AnyObject) {
let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: UIAlertControllerStyle.Alert)
namePrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Name"
})
presentViewController(namePrompt, animated: true, completion: nil)
name = //how do i copy from uialtertview textfield?
self.performSegueWithIdentifier("writelb", sender: nil)
}
这是转场:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier=="writelb")
{
let destinationVC:ViewController4 = segue.destinationViewController as ViewController4
destinationVC.score = score
destinationVC.errors = errors
destinationVC.combo = combo
destinationVC.comboH = comboH
}
我认为segue代码是正确的,因为我在应用程序的其他部分使用没有任何问题 我不知道如何使序列工作以及如何将数据从 Uialertviewtextfield 复制到字符串变量
您无法在调用 presentViewController
之后的行中获取名称,因为该调用不会阻塞。它呈现视图控制器然后继续。您应该将 segue 的代码放入 "OK" 按钮的处理程序中。由于 segue 的发送者可以是任何对象,您可以从警报中获取 textField 并将其设为发送者。然后在 prepareForSegue
中获取文本并将其发送到目标视图控制器。
@IBAction func goPressed(sender: AnyObject) {
let namePrompt = UIAlertController(title: "Enter Name", message: "You have selected to enter your name", preferredStyle: .Alert)
namePrompt.addAction(UIAlertAction(title: "OK", style: .Default, handler: {(action: UIAlertAction!) in
if let textFields = namePrompt.textFields as? [UITextField] {
// Grab the first (only) text field and perform the segue designating
// the textField as the sender.
self.performSegueWithIdentifier("writelb", sender: textFields[0])
}
}))
namePrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Name"
})
presentViewController(namePrompt, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "writelb" {
let dvc = segue.destinationViewController as ViewController4
// Get name from textField and pass it to the destination view controller.
dvc.name = (sender as UITextField).text
}
}