Kinja
Kinja

Reputation: 459

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP) Error

I'm new to Xcode and idk why i have this error, can someone help me with this? much appreciated. This is basically for users to enter their verification code given and it will display a message based on the particular code.

VerificationController

import UIKit

class VerificationController: UIViewController {

    @IBOutlet var verification: UITextField!
    @IBAction func enter(_ sender: Any) {

            if verification.text != ""
            {
                if verification.text == "123"
                {
                    performSegue(withIdentifier: "segue", sender: self)

                }
                else if verification.text == "234"
                {
                    performSegue(withIdentifier: "segue", sender: self)
                }
            }
        }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

SecondController

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet var label: UILabel!

    var myString1 = "Hello 123"
    var myString2 = "Hello 234"



    override func viewDidLoad() {
        super.viewDidLoad()
        if (VerificationController().verification.text! == "123")
            {   
                 label.text = myString1
            }
        else if (VerificationController().verification.text! == "234")
            {
                 label.text = myString2
            }


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }  
}

[IMAGE OF ERROR]

Upvotes: 1

Views: 105

Answers (1)

Imad Ali
Imad Ali

Reputation: 3301

You need to pass the verification text to the SecondViewController in prepare(forSegue) method and access the same:

In VerificationController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        let secondVC = segue.destination as! SecondViewController
        secondVC.verificationText =  verification.text
    }
}

Then in SecondViewController:

 var verificationText:String?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    if (verificationText == "123")
    {
        label.text = myString1
    }
    else if (verificationText == "234")
    {
        label.text = myString1
    }

}

Hope it helps!

Upvotes: 2

Related Questions