Sheraz Hussain
Sheraz Hussain

Reputation: 25

VC presenter is not being initialized prepare for segue IOS swift

Hello i am facing the problem as the prepare for segue function is not working properly i've the code can you please tell me how can i debug it as it has optional conditions how i can remove them and check what's the problem or what i am doing wrong as i just want a class to be initialized in target vc before the app navigates to other screen.

@IBAction func filterBtnPressed(_ sender: Any) {
    
    self.performSegue(withIdentifier: "FilterSessionVC", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "FilterSessionVC", let navigation = segue.destination as? UINavigationController, let vc = navigation.topViewController as? FilterSessionVC {
            vc.presenter = FilterSessionPresenterImplementation()
        }
    }

Upvotes: -1

Views: 57

Answers (1)

DonMag
DonMag

Reputation: 77672

Basic debugging...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    let identifier = segue.identifier
    print("identifier:", identifier)
    
    let dest = segue.destination
    print("destination:", dest)
    
    if let navigation = dest as? UINavigationController {
        print("dest IS a UINavigationController")
        
        if let vc = navigation.topViewController as? FilterSessionVC {
            print("Top Controller IS a FilterSessionVC")
            // here you can proceed
            vc.presenter = FilterSessionPresenterImplementation()
        } else {
            print("Top Controller IS NOT a FilterSessionVC")
        }
        
    } else {
        print("dest IS NOT a UINavigationController")
    }
    
}

Set a breakpoint on the first line, then step through and inspect the results.

Upvotes: 0

Related Questions