Nav button needs to go form the last view to the first view

53 views Asked by At

Visual view of project

Hello, I have provided the link above to show a visual view of what I am trying to accomplish. So each button is a segue to the next view except the last button is not which is intended. The last two views with the back buttons at the top that are automatically included because of the nav controller I want to have those back buttons go to the first view controller. How can I do this either with xcode and swift?

1

There are 1 answers

8
Reed On BEST ANSWER

If you want to remove all the navigation controller then use this code

Put this code in the button selector method

self.navigationController!.viewControllers.removeAll()

OR

navigationController?.popToRootViewController(animated: true)

OR

If you don’t have a Navigation Controller and use dismissViewController method, you still can use:

view.window?.rootViewController?.dismiss(animated: true, completion: nil)

Now comes the back button code:

override func viewDidLoad() {
    super.viewDidLoad()
    var backbutton = UIButton(type: .Custom)
    backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Any image can be used. download back button image from any site or whatever you wanna use.
    backbutton.setTitle("Back", forState: .Normal)
    backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
    backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}

func backAction() -> Void {        
   //here put code from above whichever you wanna use for example I'm using one

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