-1

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()
        }
    }

1 Answers1

0

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.

DonMag
  • 69,424
  • 5
  • 50
  • 86