塞格街上的噩梦
Nightmare on Segue street
应用应该做什么:
1) 用户在 textField 中输入一个词并点击相应的按钮
2) 应用程序应该转到另一个包含两个标签的场景。一个应该显示用户输入的单词,另一个识别点击的按钮。这些数据应该通过接收属性传递 ViewController.
实际作用:
1) 立即调用 segue,显然绕过了 prepare(segue: sender:)
2) 两个标签都是空白
3) 断点和 print()
表明 prepare(segue: sender:)
永远不会被调用
我有什么checked/tried:
1) 根据我的 print()
陈述
,按钮在情节提要中具有正确的标签
2) 我用 if else
代替了 switch
3) 我很确定我已经阅读了关于 SO
的所有相关问题和答案
4) prepare(segue: sender:)
中的 sender
从 Any?
切换到 UIButton
并再次返回
代码
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tf2: UITextField!
@IBOutlet weak var tf3: UITextField!
@IBOutlet weak var tf4: UITextField!
@IBAction func sendButton(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button2(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button3(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button4(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
func prepare(for segue: UIStoryboardSegue, sender: UIButton) {
print("Inside prepare for segue")
print("sender.tag is \(sender.tag)")
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
print("sender.tag is \(sender.tag)")
switch sender.tag {
case 101:
if (textField.text?.count)! >= 1 {
vc.staticText = "Button 101"
vc.textProp = self.textField.text!
print("sender.tag is \(sender.tag)")
}
case 102:
if (tf2.text?.count)! >= 1 {
vc.staticText = "Button 102"
vc.textProp = self.tf2.text!
print("sender.tag is \(sender.tag)")
}
case 103:
if (tf3.text?.count)! >= 1 {
vc.staticText = "Button 103"
vc.textProp = self.tf3.text!
print("sender.tag is \(sender.tag)")
}
case 104:
if (tf4.text?.count)! >= 1 {
vc.staticText = "Button 104"
vc.textProp = self.tf4.text!
print("sender.tag is \(sender.tag)")
}
default:
print("Something went wrong")
}
// print("In FirstVC, vc.textProp = \(vc.textProp)")
}else{
print("No text in textField...")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我承认我很擅长忽略显而易见的事情,但在打扰你们之前我确实花了很多时间。非常感谢任何帮助或指导...
谢谢!
prepare(for segue
没有被调用,因为签名错误。您不得更改 sender
参数的类型。
例如在额外的一行中将 sender
转换为预期类型
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue",
let button = sender as? UIButton {
...
并将随后出现的所有 sender
替换为 button
应用应该做什么:
1) 用户在 textField 中输入一个词并点击相应的按钮
2) 应用程序应该转到另一个包含两个标签的场景。一个应该显示用户输入的单词,另一个识别点击的按钮。这些数据应该通过接收属性传递 ViewController.
实际作用:
1) 立即调用 segue,显然绕过了 prepare(segue: sender:)
2) 两个标签都是空白
3) 断点和 print()
表明 prepare(segue: sender:)
永远不会被调用
我有什么checked/tried:
1) 根据我的 print()
陈述
2) 我用 if else
代替了 switch
3) 我很确定我已经阅读了关于 SO
的所有相关问题和答案4) prepare(segue: sender:)
中的 sender
从 Any?
切换到 UIButton
并再次返回
代码
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tf2: UITextField!
@IBOutlet weak var tf3: UITextField!
@IBOutlet weak var tf4: UITextField!
@IBAction func sendButton(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button2(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button3(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
@IBAction func button4(_ sender: UIButton) {
print("sender.tag is \(sender.tag)")
self.performSegue(withIdentifier: "mySegue", sender: sender)
print("sender.tag is \(sender.tag)")
}
func prepare(for segue: UIStoryboardSegue, sender: UIButton) {
print("Inside prepare for segue")
print("sender.tag is \(sender.tag)")
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
print("sender.tag is \(sender.tag)")
switch sender.tag {
case 101:
if (textField.text?.count)! >= 1 {
vc.staticText = "Button 101"
vc.textProp = self.textField.text!
print("sender.tag is \(sender.tag)")
}
case 102:
if (tf2.text?.count)! >= 1 {
vc.staticText = "Button 102"
vc.textProp = self.tf2.text!
print("sender.tag is \(sender.tag)")
}
case 103:
if (tf3.text?.count)! >= 1 {
vc.staticText = "Button 103"
vc.textProp = self.tf3.text!
print("sender.tag is \(sender.tag)")
}
case 104:
if (tf4.text?.count)! >= 1 {
vc.staticText = "Button 104"
vc.textProp = self.tf4.text!
print("sender.tag is \(sender.tag)")
}
default:
print("Something went wrong")
}
// print("In FirstVC, vc.textProp = \(vc.textProp)")
}else{
print("No text in textField...")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我承认我很擅长忽略显而易见的事情,但在打扰你们之前我确实花了很多时间。非常感谢任何帮助或指导...
谢谢!
prepare(for segue
没有被调用,因为签名错误。您不得更改 sender
参数的类型。
例如在额外的一行中将 sender
转换为预期类型
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue",
let button = sender as? UIButton {
...
并将随后出现的所有 sender
替换为 button