snksnk
snksnk

Reputation: 1595

Identify which ViewController got popped

I have a mainViewController containing a tableView and each cell pushes a specific customViewController. When it gets popped I want to trigger different function. So my question is how to I identify the viewController that got popped after it got popped. The first log reads lastObject = Optional(<Project.ViewController: 0x61900009c480>) but in general the idea does not work. Cannot find anything similar on the web. Any ideas? Thanks in advance

override func viewDidAppear(_ animated: Bool) {
print("lastObject = \(String(describing: (self.navigationController?.viewControllers.last)))")

if ((navigationController?.viewControllers.last?.isKind(of:WebViewController.self)) == true){
        print("last is web")
    
    
    }else{
print("is not web")
}

Upvotes: 0

Views: 50

Answers (2)

MMDev11070
MMDev11070

Reputation: 171

Create one protocol for all controllers something like this

protocol WhichPoppedDelegate: class {
func controllerDidPopped(controller: UIViewController)
}

assign delegates for each in mainContorller and just call that protocol before you call

navigationController?.popViewController(animated: true)

Upvotes: 0

Harish
Harish

Reputation: 2512

Only way is you could subclass the UINavigationController and override - popViewControllerAnimated. I haven't tried this yet, but possibly a solution.

Maybe not a best solution but for an temp solution: If you have have very less cell in the UITableView then you could save the ViewController name in UserDefaults when the viewController is popped and validate the name in the viewDidAppear of mainViewController

override func viewDidAppear(_ animated: Bool) {
   if (UserDefaultsValue == "FirsViewController"){
     print("last is FirsViewController")
   } else if(UserDefaultsValue == "SecondViewController") {
     print("last is SecondViewController")
   }
}

Upvotes: 1

Related Questions