从 appDelegate 启动导航控制器

Posted

技术标签:

【中文标题】从 appDelegate 启动导航控制器【英文标题】:Starting navigation controller from appDelegate 【发布时间】:2017-04-17 08:49:26 【问题描述】:

谁能告诉我如何从 ÀppDelegate? I can start arootViewContollerbut cannot start a specificUIViewControllerlike I was trying in commented code. The commented code starts the **ChooseTableViewController** but does not displayUINavigationBar` 开始 UINavigationContoller。 什么是更好的方法? 这是我的代码

- (void)setRootViewController:(NSString *)storyBoardName 
    //set the Root ViewController

    UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName 
                                                    bundle:nil];
    UINavigationController *newViewController = 
                                 [story instantiateInitialViewController];
    self.window.rootViewController = newViewController;



    /*
    ChooseTableViewController *chooseTableViewController = 
      [story instantiateViewControllerWithIdentifier:@"ChooseTableViewController"];

    self.window.rootViewController = chooseTableViewController;

    */


【问题讨论】:

ChooseTableViewController 作为UINavigationController 的r​​ootViewController 对你有用。 【参考方案1】:

Appdelegate.h

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;

Appdelegate.m

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 


     self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

       self.navigationController  = [storyboard instantiateViewControllerWithIdentifier:@"navigation"];
              UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ChooseTableViewController"];
              navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
     self.window.rootViewController =self.navigationController;
            [self.window makeKeyAndVisible];
        return YES;
    

【讨论】:

这里是什么 instanceViewControllerWithIdentifier:@"navigation"?它在那条线上崩溃了 这意味着导航控制器将是你的初始视图控制器,导航控制器的标识符是@"navigation" 但是那里崩溃了 故事板上有导航控制器吗? self.navigationController = [storyboard instantiateInitialViewController];试试这个而不是 self.navigationController = [storyboard instantiateViewControllerWithIdentifier:@"navigation"];【参考方案2】:
// Your main storyboard
UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];

// Your root navigation controller
UINavigationController *newViewController = [story instantiateInitialViewController];

// Your root view controller for root navigation controller
ChooseTableViewController *chooseTableViewController = [story instantiateViewControllerWithIdentifier:@"ChooseTableViewController"];

// Set your view controller as root view controller of your root navigation controller
newViewController.rootViewController = chooseTableViewController;

// set your root navigation controller
self.window.rootViewController = newViewController;

【讨论】:

以上是关于从 appDelegate 启动导航控制器的主要内容,如果未能解决你的问题,请参考以下文章

导航控件和其他控件是不是必须在 appDelegate 中?

从 AppDelegate、Swift 导航到 TabBarController 内的特定视图控制器

如何从 appdelegate 呈现和关闭模态视图?

使用 Swift2 从 AppDelegate 重新打开 iOS 应用程序到指定的导航控制器

从 UIView 子类推送导航控制器会导致崩溃

Monotouch:使用标签栏控制器和导航控制器。 AppDelegate 中有哪一个?