0

I want to pass data from a view controller to another but I've a Navigation controller between them, so there's a "present" function instead a "performSegue".

 private func IniciarMainAdmin(){
    let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
    guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else{
        return
    }
    present(mainAdminNVC, animated: true, completion: nil)
}

I've tried with this code but it didn't work for me:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destViewController = segue.destination as! NCMainAdmin
        let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
        secondViewcontroller.adminUserData = "This is a test"
}

Notes:

  • NCMainAdmin is the Navigation Controller
  • NCMenuAdmin is the first View Controller of the Navigation Controller

Thanks!

1 Answers1

1

For your code prepare(for to trigger , you need to create a segue from the sender vc to the navigation , so either

 private func IniciarMainAdmin(){
    let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
    guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else{
        return
    }
    let first = mainAdminNVC.viewControllers.first as! NCMenuAdmin
    first.adminUserData = "This is a test"
    present(mainAdminNVC, animated: true, completion: nil)
}

OR

self.performSegue(withIdentifier:"id",sender:nil)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destViewController = segue.destination as! NCMainAdmin
        let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
        secondViewcontroller.adminUserData = "This is a test"
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87