如何确定UIViewController是否是根视图控制器?

时间:2021-12-03 22:10:25

basically I want to know if the view controller I'm in is the root view controller or not.

基本上我想知道我所在的视图控制器是否是根视图控制器。

If its not I want to put a button in the nav bar that says "back" (as if it were a proper back button - this bit I know how to do).

如果它不是我想在导航栏中放一个按钮,显示“返回”(好像它是一个正确的后退按钮 - 这一点,我知道该怎么做)。

Before you ask, I have removed all the titles from my view controllers - I didn't want them to show up on my navigation bar... its very complicated - but this means that when I go through my navigation stack none of the pushed view controllers have a back button. :/

在你问之前,我已经从我的视图控制器中删除了所有标题 - 我不希望它们显示在我的导航栏上......它非常复杂 - 但这意味着当我浏览导航堆栈时没有被推送视图控制器有一个后退按钮。 :/

Thanks Tom

谢谢汤姆

2 个解决方案

#1


67  

if ( self != [self.navigationController.viewControllers objectAtIndex:0] )
{
   // Put Back button in navigation bar
}

#2


9  

You can also try:

你也可以尝试:

if (self.navigationController.viewControllers.count == 1) {
    NSLog(@"self is RootViewController");
}

#1


67  

if ( self != [self.navigationController.viewControllers objectAtIndex:0] )
{
   // Put Back button in navigation bar
}

#2


9  

You can also try:

你也可以尝试:

if (self.navigationController.viewControllers.count == 1) {
    NSLog(@"self is RootViewController");
}