krikor Herlopian
krikor Herlopian

Reputation: 731

remove view controller from navigation controller

I have 5 view controllers in navigation controller, and I want to remove Pages three and four on page five.The problem with below code is, if i remove index 3 and index 4 and I am on page Five.I get no back button on top anymore.I should be getting back button to page 2 again.But no. what is the solution? Thank You for all the help provided.Appreciated. This error really driving me crazy

navigationController!.viewControllers.remove( at: 3 ) navigationController!.viewControllers.remove( at: 4 )

Upvotes: 0

Views: 2852

Answers (1)

Wez
Wez

Reputation: 10712

I think a cleaner way to do this is to modify the array of viewControllers and then set them back on the navigation controller like this..

if let nav = self.navigationController {
    var stack = nav.viewControllers
    // index starts at 0 so page three index is 2
    stack.removeAtIndex(2)
    stack.removeAtIndex(3)
    nav.setViewControllers(stack, animated: true)
}

I just tested this on one of my navigation stacks and the back button was retained, I assume this is due to the setViewControllers method doing it's thing and setting up the stack for you.

Upvotes: 3

Related Questions