skip to Main Content

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")
}

2

Answers


  1. 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")
       }
    }
    
    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search