如何在AppDelegate中将视图控制器设置为根视图控制器

时间:2016-08-11 13:04:01

标签: ios objective-c uiviewcontroller

我有一个带导航控制器的视图控制器。它的名字是LoginViewController。在我的AppDelegate中,我希望将LoginViewController保留为根视图控制器。

如何在Objective-C中执行此操作?如何将视图控制器设置为根视图控制器?

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

3 个答案:

答案 0 :(得分:2)

喜欢

- (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;
}

对于E.g

enter image description here

答案 1 :(得分:0)

没有故事板:

- (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;
}

如果使用故事板,只需将该视图控制器作为故事板中的初始视图控制器。

enter image description here

答案 2 :(得分:0)

如果要从登录页面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];
相关问题