关于Main Interface 的解释

关于Main Interface 的解释

通常我们建立一个新的项目xcode 都会为我们创建一些默认的文件及项目工程配置,从xcode6 开始就不能建立一个空的项目模板,界面的显示主要依赖于window对象的存在,也只有window能够主动显示,其他的视图view可以添加到UIWindow上

说了这么多那么接下了就细说下 main interface 的实际作用吧!
点击 prject 项目 --》General,将Main Interface 改为空。如图:

关于Main Interface 的解释_第1张图片
默认创建配置

程序运行将会黑屏。因为没有实现UIWindow

关于Main Interface 的解释_第2张图片
黑屏状态

在AppDelegate.m中添加window的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

此时运行程序会发现有错误,错误提示如下:

‘NSInternalInconsistencyException‘, reason: ‘Application windows are expected to have a root view controller at the end of application launch‘
提示没有rootViewController。需要设置UIWindow对象的rootViewController才能实现程序的运行显示。

设置UIWindow的rootViewController代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 设置rootViewController
    ViewController *controller = [[ViewController alloc] init];
    self.window.rootViewController = controller;
    
    return YES;
}

运行程序,会看到和没有修改Main Interface 之前是一样的。

你可能感兴趣的:(关于Main Interface 的解释)