Amal El-galant
Amal El-galant

Reputation: 139

Getting previous UIViewController swift

I want to get previous viewController.

I have 2 UIViewControllers and navigationMenu when I select any row from navigation menu i want to get UIViewController from where menu opened to move to the second UIViewController.

I want to know how to get previous UIViewController in this case

I tried this but count return nil:

 let n: Int! = self.navigationController?.viewControllers.count
 print(n)
 let myUIViewController = self.navigationController?.viewControllers[n-2] as! UIViewController

Edit:-

opening menu code

  let menuButton = UIBarButtonItem.init(title :"open", style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
    navigationItem.leftBarButtonItem = menuButton

Upvotes: 1

Views: 2871

Answers (5)

Alessandro Ornano
Alessandro Ornano

Reputation: 35402

Swift 5.x

Suppose you are in SecondViewController and you want to check if the previous view controller is really FirstViewController, so you can do:

if let prevIndex = self.navigationController?.viewControllers.firstIndex(where: {$0 == self })?.advanced(by: -1) {
   if let prevVC = self.navigationController?.viewControllers[prevIndex], prevVC is FirstViewController {
      // do whatever you want with prevVC
   }
}

Upvotes: 0

Frank Eno
Frank Eno

Reputation: 2649

if your ViewControllers are embedded into a NavigationController, you can use the standard popViewController function in your second VC:

_ = navigationController?.popViewController(animated: true)

Upvotes: 1

Amal El-galant
Amal El-galant

Reputation: 139

I solved my problem by saving UIViewController from where menu opened in delegate so i can access it from anywhere

Upvotes: 0

Sudipto Roy
Sudipto Roy

Reputation: 6795

Add a variable of First ViewController type in Second ViewController .

class SecondViewController : UIViewController 
{
   var previousVC = FirstViewController()
}

Now perform segue to Second ViewController from First ViewController .

 performSegue(withIdentifier: "to2nd", sender: self) //to2nd or Your Choice

In prepare segue function put your First view Controller object into target .

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{
        if segue.identifier == "to2nd" // Or you segue id name 
        {
            if let target = segue.destination as? SecondViewController
            {
                target.previousVC = self
            }
        }
}

And now whenever you need previous ViewController or its data , use previousVC

Upvotes: 1

Rashwan L
Rashwan L

Reputation: 38833

This is how you can get the previous viewController, check the comments for explanations:

// get all the viewControllers to start with
if let controllers = self.navigationController?.viewControllers {
    // iterate through all the viewControllers
    for (index, controller) in controllers.enumerated() {
        // when you find your current viewController
        if controller == self {
            // then you can get index - 1 in the viewControllers array to get the previous one
            let myUIViewController = controllers[index-1]
            print(controllers[index-1])
        }
    }
}

Upvotes: 4

Related Questions