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-p开发者_如何学Goushed, then pop back, else do nothing
How can I tell if the view has been pushed?
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
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.
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.
精彩评论