59.加载Viewcontroller的几种方法(添加导航,解决xib里面空间不显示问题)

// 一、根据StoryboardID(需要在Storyboard设置),通过ViewController所在的Storyboard来加载:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"FicowVC"];


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

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

    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"FicowVC"];

    

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

    

    return YES;

}




// 二、通过UIViewController对应的.xib文件加载:
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"FicowVC" bundle:nil];




// 三、直接加载UIViewController类:
UIViewController *vc = [[UIViewController alloc] init];



/*
注意:
loadNibNamed和initWithNibName需要加载的xib文件是不一样的。
initWithNibName需要加载的xib文件的File Owner应改是需要加载的类,
而loadNibNamed需要加载的xib文件的File Owner为NSObject。
*/
原文地址:https://www.cnblogs.com/qiangzheVSruozhe/p/9395756.html