Home > Blockchain >  Pass data from presented View in Navigation Controller, back to custom view (XIB)
Pass data from presented View in Navigation Controller, back to custom view (XIB)

Time:09-16

Im using a custom XIB file as a popover for a view controller. The XIB file contains two buttons = "send" and "add additional fields". Selecting the button to add additional fields presents another view controller that's embedded in a Navigation controller. When the user lands on that controllers screen, and selects their extra options, that data should be passed back to the XIB's var extraOptions = [String]() field. From there, the user should be able to upload their post by selecting "send".

The problem Im having is data is not being passed back to the XIB from the view controller embedded in the navigation controller.

Is this even possible? I've tried every solution I could think of: protocol/delegate method, setting the var extraOptions field in the XIB file, from the presented controller before dismissing it and returning back to the XIB, and this is my latest attempt:

Custom XIB named CreatePostModal(view controller thats a pop over)

@objc func addExtraOptions(_ sender: UITapGestureRecognizer? = nil) {
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "ExtraOptionsNavigationController") as! UINavigationController
    vc.delegate = self
    vc.modalPresentationStyle = .fullScreen
    self.present(vc, animated: true, completion: nil)
}

ExtraOptionsViewController

@IBAction func donePressed(_ sender: Any) {
    if let navController = presentingViewController as? UINavigationController {
        let presenter = navController.topViewController as! CreatePostModal
        presenter.extraOptions = self.extraOptionsSelected
        print("These are the options selected ==> \(present.extraOptions)") //this prints the options and shows that they're there, but the array is empty whenever a user tries to upload the data
    }
    dismiss(animated: true, completion: nil)
}

As you can see, in the print method, it prints the variable and shows that the data is there, but when returning the XIB file, and the user saves & uploads the data, the field is showing up as empty.

How do I pass data back to a XIB from a view embedded in a navigation controller?

CodePudding user response:

You can use one in three ways:

  • NSNotificationCenter
  • Delegate
  • Closure
  • Related