关于Main Interface 的解释

关于Main Interface 的解释

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

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

默认创建配置

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

黑屏状态

在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 之前是一样的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容