3

What the app should do:

1) The user types a word into a textField and taps the corresponding button

2) The app should segue to another scene containing two labels. One should display the word typed by the user, the other identifies the button tapped. These pieces of data should be passed via properties on the receiving ViewController.

What it actually does:

1) The segue is immediately invoked, apparently bypassing prepare(segue: sender:)

2) Both labels are blank

3) Breakpoints and print() indicate that prepare(segue: sender:) is never called

What I've checked/tried:

1) The buttons have the correct tags in storyboard, according to my print() statements

2) I've substituted if else for switch

3) I'm pretty sure I've read every associated question and answer on SO

4) Switched sender in prepare(segue: sender:) from Any? to UIButton and back again

The code

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.
    }


}

I admit upfront that I'm pretty good at overlooking the obvious, but I did put in a lot of time before bugging you guys. Any help or direction will be very much appreciated...

Thanks!

rattletrap99
  • 1,469
  • 2
  • 17
  • 36

1 Answers1

3

prepare(for segue is not called because the signature is wrong. You must not change the type of the sender parameter.

Cast sender to the expected type in an extra line for example

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

and replace all subsequent occurrences of sender with button

vadian
  • 274,689
  • 30
  • 353
  • 361
  • @vadian-- Thanks so much, sir! The difference I see between your code and mine is the use of "Any?" vs "UIButton," and the use of "button" in place of "sender." I have actually tried the first part several times, but continued to use "sender." I'll have to go over this a few times to get my head around it. Thanks again! – rattletrap99 Oct 29 '19 at 18:33