2

I have a tab bar driven app.

The app has some view controllers that are stacked on each other the usual way. Some of these view controllers may be top level - meaning instantiated directly from the tab bar controller. Those do not have a "back" button in their navigation bar.

Some of these can be called at many points within the call stack. Meaning they could be called from some other view controller and not directly from the tab bar.

I am looking for a smart way to determine programatically whether the current view controller currently has a "back" button in its nav bar or not. (If it does not then I want to display some other bar item at that place.)

Any hint is appreciated. :)

4 Answers 4

5

Well... from what you are describing, I can understand that you have a UITabBarController with a list of view controller in each tab, and each view controller is a navigation controller, which further leads to new views. And you are interested in finding if a particular view is the first view or the second view, and so forth.

Interestingly, there is a way to do this. The navigation controller is basically a viewController array with views added to the index of the navigationController. The first view will be in index 0. If you perform a pushViewController, the newViewController is placed at index 1.

Just check for the index count of the self.navigationController of that particular view. If it is greater than 1, you can be certain that it is not the initial view.

You can also check for the backBarButtonItem property, but then again, it may not be needed. The viewControllers array count should do.

2
  • Hi legolas and jemmons, the index count on the view controller's stack sounds reasonable. I will try thand and will find the answer to the next quesion myself. However, you may know out of the top of your head. Considering that there is a UITabBarController on the very top, would you expect that the index for each view controller on of each tab's stack is 0 or 1? Thanks so far. Nov 23, 2011 at 18:07
  • The tabBarController is the rootViewController. When you use a navigationController, each view is internally assigned to each index position of the stack. This is a property of the navigationController, which you embed in each tab of your view. This has nothing to do with the tabBarController.
    – Legolas
    Nov 23, 2011 at 22:43
3

You could ask your view controller for it's -navigationController, ask the navigation controller for its -viewControllers stack, and see what the -count of that stack is.

The currently-showing view controller is the "last" item in the stack (index n-1). The "back" view controller second-to-last (n-2 — n being the count of the stack). So if the count of the stack is 1, that means you've reached the top. There's nothing in the stack to go back to and no back button will be displayed.

2

Consider just getting the index of the view controller within the navigation controller's viewControllers array property. If it's zero, then the view controller is the top controller (without a back button).

0
0

UINavigationController has topViewController and visibleViewController properties which could help you out. If you just want to know if there is a back button or not you can check the backBarButtonItem property of the UINavigationItem and see if it is non-nil and non-hidden.

3
  • Though comparing topViewController and visibleViewController won't necessarily let you know if you're at the top of the stack if the view controller on the top appears multiple times in the stack.
    – jemmons
    Nov 22, 2011 at 23:33
  • 1
    You can not add the same UIViewController object to the stack multiple times (if you try you'll get a crash, see stackoverflow.com/questions/7083124/…). You can add multiple instances of the same UIViewController subclass. So if topViewController == visibleViewController then the visibleViewController is at the top.
    – jbat100
    Nov 22, 2011 at 23:40
  • Well, yes, if there were the same view controller on that stack twice then it were two (or moer) instances of the same view controller class but not the same view controller object. So far that is clear. However, my understanding is, that the tab bar's view controller is on top of that stack. Is that correct? --- Checking on the existance of the navigation item that corresponds to the back button sounds reasonable. Nov 23, 2011 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.