views:

40

answers:

3

I've got a view that sometimes appears as a pushed view of uinavigationcontroller, and sometime just as the initial view of a tab-bar item.

There's a 'Save' button on the interface which I want to make the view pop back to previous view when it's been pushed onto screen, and do nothing when it's being displayed as part of a tab bar selected screen.

In pseudo-code, I guess what I want to do is:

if view-has-been-pushed, then pop back, else do nothing

How can I tell if the view has been pushed?

+1  A: 

As per documentation

 NSArray* views = [myNavigationController viewControllers];
 if (self == [views objectAtIndex:0])
 {
    // I am the root view
 }

but as jasarien said, popViewControllerAnimated does nothing anyway if the view is already the root

Harald Scheirich
This isn't an ideal solution, as regardless of whether the view has been pushed or is the root, the only time the button will be able to be pressed is when the view is visible, which will mean it is the top view controller. It is possible for the view controller to be the top view controller AND the root view controller at the same time.
Jasarien
Sorry eating crow here, did not read the docs correctly ...
Harald Scheirich
+1  A: 

You could get the view controllers property from the navigation controller and compare against the first controller in the array. If the comparison returns true, then it's the root view controller, else it's been pushed.

However, if a view controller is the root view controller, calling pop should just not do anything, so you shouldn't need any extra logic.

Jasarien
+1  A: 

Your "if view-has-been-pushed, then pop back, else do nothing" logic is easily implemented with something like:

if (self.navigationController != nil) {
    // We are part of a navigation controller, so pop
}

You probably want to remove the Done button if you are not in a navigation controller? You can do the same check in viewDidLoad and show or hide the Done button there.

St3fan