1

I am inserting a ViewController into my current stack. And trying to perform popviewcontroller like this.

 var viewControllersArray: [UIViewController] = self.navigationController!.viewControllers


    if(dm.isLoginViewLoaded)
    {
        print("Login view already loaded")

    }

    else
    {
        print("Login view havent load before")
        let myviewcontroller:LoginViewController=UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LOGIN") as! LoginViewController
        let index=viewControllersArray.count-2
        viewControllersArray.insert(myviewcontroller, at: index)
        // update navigationController viewControllers
        self.navigationController!.setViewControllers(viewControllersArray, animated:false)

    }
    self.navigationController?.popViewController(animated: true)

But why I cant achive what im trying to do. Even in this second condition satisfying my viewcontroller always pushing to Root viewcontroller. I want to add a new viewcontroller in between my current view controller and root viewcontroller, and perform the pop operation. Please help me. Thanks

user1960169
  • 3,533
  • 12
  • 39
  • 61

2 Answers2

0

Try this code:

let arrController:[UIViewController] = (self.navigationController?.viewControllers)!//array of all controllers in navigation stack
    for vc in arrController
    {
      if (type(of: vc)  == type(of: LoginViewController
))//check exist or not
      {
        self.navigationController?.popToViewController((arrController[(arrController.index(of: vc))!]), animated: true)
      }
    } 
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
  • I dont want to check if it exists or not.. I know in it loads without loginviewcontroller. So I want to insert it manually and popto it – user1960169 Jul 13 '17 at 13:40
0

I think you simply "mis-counted" the array position. Your index should be:

let index = viewControllersArray.count - 1   // *not* -2

However, this may be a little more "robust" method:

@IBAction func popToLogin(_ sender: Any) {

    if let navVC = self.navigationController {

        var bFoundLogin = false

        var viewControllersArray = navVC.viewControllers

        for vc in viewControllersArray.reversed() {
            if vc is LoginViewController {
                // we found LoginViewController in stack, so
                //  pop to it (even if it's multiple levels back)
                bFoundLogin = true
                navVC.popToViewController(vc, animated: true)
                // found it, so break out of the for loop
                break
            }
        }

        if !bFoundLogin {

            // we didn't find LoginViewController in stack, so
            //  replace current VC with LoginVC

            //if let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LOGIN") as? LoginViewController {
            if let loginVC = UIStoryboard(name: "AnimConstraints", bundle: nil).instantiateViewController(withIdentifier: "LOGIN") as? LoginViewController {

                // insert loginVC before last item
                viewControllersArray.insert(loginVC, at: viewControllersArray.count - 1)
                // set the NavVC's array of controllers
                navVC.setViewControllers(viewControllersArray, animated: false)
                // pop back one level (to our inserted loginVC)
                navVC.popViewController(animated: true)

            }

        }

    }

}

Using a loop to "find" LoginViewController in the stack allows you to "pop to it" even if it's not the "next one in line", and also removes the need for your dm.isLoginViewLoaded tracking (unless you're using that elsewhere).

DonMag
  • 69,424
  • 5
  • 50
  • 86