0

The Link below shows my Storyboard and I think my problem is a lot easier to understand with the image. My tabBarController manages a tab which is embedded in a navigationController. UIButton "start" leads to the next view by a show segue (blue arrow). On this view a bar button item leads to the last viewController again with a show segue (green arrow). From the last view controller i want to go back to my initial one, again by a bar button item (black arrow). But now there is the Problem, that my initial view controller shows a "back button" on the top left (of course, because by the black arrow the initial view is just added to the navigation stack, right?!). I solved this by going back to the tab bar controller (red arrow). Now there is no "back" button any more but now i am wondering if there isn't like a stack of my navigation views still existing in the background and wasting memory or something like that. How can i delete this stack or is it even existing? (maybe i got this completely wrong) Or is there even a better way to go back to my first view?

Sorry for the unprofessional description of my problem but i just started coding so i don't have the right words for some of the issues yet.

thank you so much!

my Storyboard looks like this

1
  • Use an unwind segue Commented Nov 7, 2016 at 23:14

1 Answer 1

1

If I understood it correctly, you want your last view controller to navigate to the initial view controller of the navigation stack. On your last view controller, you can add this to your code:

       override func viewDidLoad() {
    super.viewDidLoad()
    //this will add a button to top right of the nav bar, 
    //change "ButtonName" to a title you want
    //this button will call the blackArrow function
    self.navigationItem.rightBarButtonItem = UIBarButtonItem (
        title: "ButtonName", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.blackArrow))
}


func blackArrow() {
//this function navigates to the initial view controller of the navigation controller
if let navigationController = self.navigationController {
    navigationController.popToRootViewController(animated: true)

    }
} 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! And do i have to delete the stack of viewcontrollers or is the stack reduced to only the root view controller if i go back to that one?
You don't have to delete the stack of viewcontrollers if you need them, and it is not reduced to only the root view controller. The navigation controller just displays the initial view, and you can navigate other view controllers connected to it, like the first time such as the one in blue then green arrow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.