【问题标题】:Set RootViewController in UINavigationController class - Objective C在 UINavigationController 类中设置 RootViewController - Objective C
【发布时间】:2020-11-10 04:15:27
【问题描述】:

我是使用 Objective C 的新手,我对故事板有很好的了解,但现在我尝试通过代码更改 UINavigationController 的根视图控制器,碰巧它取决于一个变量,我将设置根,但我除了我的 UINavigationControllers 自定义类之外,不知道如何配置此操作。我希望你能帮助我。请记住,此视图是应用程序中的第三个屏幕。

@interface PCCatalogNavigationController () <UINavigationControllerDelegate>

@end

@implementation PCCatalogNavigationController

- (void)viewDidLoad{
    if (status == 1){
        //TableViewViewController
    } else {
        //Optional StarViewController
    }
}

@end

【问题讨论】:

    标签: ios objective-c uinavigationcontroller rootviewcontroller


    【解决方案1】:

    在您的应用委托中添加您的导航控制器的属性

    @property (nonatomic , strong) UINavigationController *navigationController;
    

    里面

    -(BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    
    TableViewViewController *TableViewView = [[UIStoryboard
    storyboardWithName:@"Main" bundle:nil]
    instantiateViewControllerWithIdentifier:@"TableViewViewController"];
    
    
    StarViewController *StarView = [[UIStoryboard
    storyboardWithName:@"Main" bundle:nil]
    instantiateViewControllerWithIdentifier:@"StarViewController"];
    
    
        if (status == 1){  
        self.navigationController =
        [[UINavigationController alloc]
        initWithRootViewController:TableViewView];  
        } else {  
        //Optional
        StarViewController  self.navigationController =
        [[UINavigationController alloc]
        initWithRootViewController:StarView];  
        }  
    }
    

    您可以使用自定义导航控制器代替 UINavigationController

    【讨论】:

    • format your answer 至少让它可读,谢谢。
    • 请更好地解释您的答案。我更新了我的问题。 @zrzka
    • 此代码不起作用。您将 instanciateViewControllerWithIdentifier:@"TableViewViewController" 两次投射到不同的 ViewController 类,并分配相同的 UINavigationController。所以代码总共写了两次,除了在分配中丢失的转换之外没有任何影响。而且该方法不返回任何东西
    • 这是我这边的拼写错误@OlSen,谢谢队友
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2011-11-28
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多