详解iOS的UI开发中控制器的创建方法

2020-01-14 17:04:46王旭

#import "YYViewController.h"

 

@implementation YYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //1.通过xib创建控制器
    YYViewController *controller=[[YYViewController alloc]initWithNibName:@"two" bundle:nil];
    //2.设置这个控制器为Window的根控制器
    self.window.rootViewController=controller;
    
    [self.window makeKeyAndVisible];
    return YES;
}

 


(2)两个错误注意点

 

1)不能加载(was unable to load a nib named "two")

产生原因:在xib文件中没有进行任何操作。

解决方法:往xib中拖一个view

2)加载了xib,但是没有设置输出口(loaded the "two" nib but the view outlet was not set.')

产生原因:没有把xib的view设置为YYviewController的view

解决方法:设置File‘s Owner,可以理解为设置这个文件归谁所有,对File‘s Owner和view进行连线。连线是因为一个xib中可能会有多个view,在storyboard中默认就已经进行了连线。

详解iOS的UI开发中控制器的创建方法

四、模仿有storyboard的项目控制器的创建