正确的方法通过'UINavigationController'子类设置'Root ViewController'

时间:2017-07-14 14:17:08

标签: ios swift uinavigationcontroller rootviewcontroller

我正在尝试动态,使用代码通过他的子类更改Root UINavigationController

基本上,我的 Storyboard 看起来像这样:

#MARK:App Storyboard

enter image description here

如您所见,我将CustomNavigationController设置为Initial View Controller选项(通过Storyboard)。

我如何通过CustomNavigationController课程设置将在我运行应用时显示的根ViewController

#MARK:'CustomNavigationController'类

class CustomNavigationController: UINavigationController {

  // What method should i use?

}

1 个答案:

答案 0 :(得分:2)

右侧的两个视图控制器必须具有故事板标识符(在身份检查器选项卡上)。

然后在您的自定义NavVC中,在根目录下构建您想要的视图控制器,并使其成为导航堆栈中唯一的视图控制器(这是一个数组)...

- (void)viewWillAppear:animated {
    [super viewWillAppear:animated];
    UIStoryboard *storyboard = [self storyboard];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"one of the ids you set up"];
    self.viewControllers = @[ vc ];
}

快速(非常肯定)......

override func viewWillAppear() {
    super.viewWillAppear()
    let storyboard = self.storyboard
    let vc = storyboard.instantiateViewController(withIdentifier: "one of the ids you set up")
    self.viewControllers = [ vc ] 
}