0

I am facing problem in which I have four view controller on Main Screen. If the User directly go to second view controller from Main VC. I am unable to navigate to the first VC using popToViewController, because in memory we have only the two ViewController which is Main VC and my second VC.

How to navigate to first VC which still not initialised?
The Code is as below :

var viewControllersArray : NSArray = self.navigationController!.viewControllers!
var count = viewControllersArray.count
var i = 0
for i ; i < count ; i++ {
    var obj = viewControllersArray.objectAtIndex(i) as! UIViewController
    if obj.isKindOfClass(ThirdVC) {
        self.navigationController?.popToViewController(obj as UIViewController, animated: true)
    }
}

Please suggest the solution. Thanks in advance.

Note : I am doing the instantiateViewControllerWithIdentifier but problem is that it navigate to my second VC which not accepted.

Kerberos
  • 4,036
  • 3
  • 36
  • 55
yo2bh
  • 1,356
  • 1
  • 14
  • 26
  • You can't pop to a view controller that hasn't been initialised and is not in the navigation stack. It is also not going to be a great experience for the user to navigation from a view "A" to view "C" then go "back" to view "B". It sounds like you may want to rethink the navigation strategy for this app. – Steve Wilford Sep 10 '15 at 12:16

1 Answers1

0

Use the below code . May help it.

var loginController: LoginViewController = LoginViewController(nibName:   
"LoginViewController", bundle: nil)
var vcs: [AnyObject] = 
NSMutableArray.arrayWithArray(self.navigationController.viewControllers)
if vcs.containsObject(loginController) {
   self.navigationController.popToRootViewControllerAnimated(true)
   return
}
vcs.insertObject(loginController, atIndex: vcs.count() - 1)
self.navigationController.setViewControllers(vcs, animated: false)
self.navigationController.popViewControllerAnimated(true)

If View controller is not added in navigation stack then first add it into navigation stack and then pop that view controller.

Hardik Shekhat
  • 1,680
  • 12
  • 21
  • I am using customise navigationItem here, due to this it is possible. To navigate from third VC to First one. But it take time when we create instantiateViewControllerWithIdentifier. dont know why it take times. – yo2bh Sep 10 '15 at 13:36
  • If helpful then accept and upvote it then other user can find it as helpful. – Hardik Shekhat Oct 13 '15 at 07:10