I know that there are few questions and answers regarding this topic, but believe me when I say that I have checked them before posting this questions.
I am trying to pass information to a controller (which was the previous one shown). As a summary, I am trying to send information from the second controller, SearchViewController
, to Tthe first controller MainViewController
(which is embedded in a navigation controller).
I have tried several ways to do it, but no one is exactly what I am looking for. I will paste each of them, and the explanation of the problem:
Note
Please note that I am avoiding using segue
, so it is not an option.
Attempt 1
The navigation is correctly made. It is shown as full screen, and not a popping window, what is correct. Nevertheless, it does not keep the changes done for coordinate
and it takes the default value, what makes me believe that it is a new instance and not the one I am trying to navigate to.
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Full screen. Navigation controller shown. Does not keep the changes in variables
let vc = MainViewController()
vc.setCoordinate(coordinate: self.coordinate!)
navigationController?.popToRootViewController(animated: true)
} else {
showToast(message: "Please, enter the coordinate")
}
}
Attempt 2
The navigation is correctly made and keeps the changes made on coordinate.
Nevertheless, it is shown as a popping window and the navigation controller is not shown anymore.
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Pop window. Not full screen. Navigation controller not shown. Keeps the variable values
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
mainViewController.setCoordinate(fcoordinate: coordinate!)
self.present(mainViewController, animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
Attempt 3
I used this approach because I am trying to come back to the previous controller, but dismiss
is not navigating anywhere, so it is failing.
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Does not come back to the previous controller
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
mainViewController.setCoordinate(coordinate: coordinate!)
self.dismiss(animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
Attempt 4
This approach does not allow me to access the methods/variables of mainViewController
so I can not change the value of coordinate
. Furthermore, the ViewController
is showing as a popping window and not in full screen
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Pop window. Not full screen. No access to methods
guard let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainViewControllerID") else { return }
let navController = UINavigationController(rootViewController: vc)
self.navigationController?.present(navController, animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
Attempt 5
Navigation is correct. It is in full screen, what is good, but navigation controller is not shown. It does not keep the changes made in coordinate
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Full screen. Navigation controller shown. Does not keep the changes in variables
let vc = MainViewController()
vc.setCoordinate(coordinate: self.coordinate!)
self.navigationController?.popViewController(animated: true)
} else {
showToast(message: "Please, enter the coordinate")
}
}
All your attempts instantiate a new vc other then the real presented previous one marked with let vc = MainViewController()
or storyBoard.instantiateViewController
so you need
let arr = self.navigationController!.viewControllers
let prev = arr[arr.count - 2] as! MainViewController
prev.setCoordinate(coordinate: self.coordinate!)
self.navigationController!.popToRootViewController(animated: true)