【问题标题】:How to set the view controller as root view controller in AppDelegate如何在 AppDelegate 中将视图控制器设置为根视图控制器
【发布时间】:2016-08-11 13:04:01
【问题描述】:

我有一个没有导航控制器的视图控制器。它的名字是LoginViewController。在我的AppDelegate 中,我想保留我的LoginViewController 作为根视图控制器。

如何在 Objective-C 中做到这一点?如何将我的视图控制器设置为根视图控制器?

注意:我的视图控制器没有导航视图控制器。它是一个单一的视图控制器。

【问题讨论】:

标签: ios objective-c uiviewcontroller


【解决方案1】:

喜欢

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. get the Storyboard Name
UIStoryboard* main = [UIStoryboard storyboardWithName:@"Main"
                                               bundle:[NSBundle mainBundle]];
//2. get the ViewController using Storyboard ID
UIViewController *viewConr = [main instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
// 3.finally assign the Root
self.window.rootViewController = viewConr;
[self.window makeKeyAndVisible];
return YES;
}

例如

【讨论】:

    【解决方案2】:

    没有故事板:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        LoginViewcontroller *Vc = [[LoginViewcontroller alloc]init];
        self.window.rootViewController = Vc;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    如果使用情节提要,只需将该视图控制器作为情节提要的初始视图控制器。

    【讨论】:

      【解决方案3】:

      如果你想从登录页面 VC 中将主页 VC 设置为 root vc

      在登录页面导入appdelegate

      #import "AppDelegate.h"//Import in Login page VC
      self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; // In viewDidLoad
      

      在你导航的地方写下代码

      //Make root view controller
      UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
      HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Home"];//Your Home page story board ID
      self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
      [self.delegate.window makeKeyAndVisible];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 2017-10-23
        相关资源
        最近更新 更多