开发者

Finding out where I am in uinavigationcontroller hierarchy, or if view has been pushed?

开发者 https://www.devze.com 2023-01-01 07:07 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消