Segue街上的噩梦

问题描述 投票:3回答:1

应用程序应该做什么:

1)用户在textField中键入一个单词,然后点击相应的按钮

2)应用程序应选择另一个包含两个标签的场景。一个应显示用户键入的单词,另一个应标识所点击的按钮。这些数据应该通过接收ViewController上的属性传递。

它的实际作用:

1)立即调用segue,显然绕过了prepare(segue: sender:)

2)两个标签都为空白

3)断点和print()表示从不调用prepare(segue: sender:)

我检查/尝试过的内容:

1)根据我的print()陈述,按钮在情节提要中具有正确的标签

2)我用if else代替了switch

3)我很确定我已经阅读了所有与之相关的问题和答案

4)将sender中的prepare(segue: 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.
    }


}

我承认,我很擅长于忽略明显的东西,但是在给你们打扰之前,我确实花了很多时间。任何帮助或指示将不胜感激...

谢谢!

ios swift properties switch-statement uistoryboardsegue
1个回答
3
投票

prepare(for segue未调用,因为签名错误。您不得更改sender参数的类型。

例如,在额外的行中将sender投射到期望的类型

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue",
       let button = sender as? UIButton {
       ...

并将sender的所有后续出现替换为button

© www.soinside.com 2019 - 2024. All rights reserved.