Mike C.
Mike C.

Reputation: 31

prepare function cause crash

In my swift app I've the following prepare function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination = segue.destination as? LocalPictureCVC {
        destination.selectedLocal = selectedLocal
    }
}

But when I try to use selectedLocal in LocalPictureCVC it crash due to selectedLocal is nil.

How can I solve this problem?

Upvotes: 0

Views: 106

Answers (2)

V D Purohit
V D Purohit

Reputation: 1189

Try this I hope it will help you:-

first make sure to give segue properly with identifier then code like below then in first ViewController

var valueToPassInSecondVC = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //NOTE:- make sure about your identifier is correct as well as destination ViewController
    if segue.identifier == "yourIdentifierOfSegue" {
        let destVC = segue.destination as! ContainerViewController
        destVC.valueFromFirstVC = self.valueToPassInSecondVC
    }
}

in your destination class

var valueFromFirstVC:String = ""
//In my case pass String from FirstViewController to SecondViewController

thanks,I hope it will help you.

Upvotes: 1

Mahgolsadat Fathi
Mahgolsadat Fathi

Reputation: 3325

try checking your segue identifier in your prepare function

if segue.identifier == "Something"

Upvotes: 0

Related Questions