ios-创建根视图控制器的三种方式

1、纯代码创建根视图控制器,在Appdelegate中的didFinishLaunchingWithOptions
self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //设置窗口根窗口控制器
    self.window.rootViewController
    //将窗口作为主窗口被设置可见
    [self.window makeKeyWindow];
2、通过storyboard文件来创建,加载storyboard中的控制器就可以了。从这个文件中实例化控制器。当你自己创建了个storyboard可以这样进行设置,但是你要保证你的这个storyboard文件不为空,以及设置了Is Initial View Controller.如果我们是用系统自带的就不要去修改了还是直接用就可以了。

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //加载文件
    UIStoryboard * board=[UIStoryboard storyboardWithName:@"ZXStoryboard" bundle:nil];//nil就代表在mainBundle这个路径下
    //从storyboard中实例化控制器
    UIViewController * viewController=[board instantiateInitialViewController];
    self.window.rootViewController=viewController;
    [self.window makeKeyAndVisible];
3、xib创建控制器,先创建自定义控制器类,然后指定xib文件,修改xib文件中的File's Owner的custom class,设置为你自定义的控制器类,
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
XibViewController * xibVc=[[XibViewController alloc]initWithNibName:@"ZXib" bundle:nil];
self.window.rootViewController=xibVc;
[self.window makeKeyAndVisible];

 


作者:清风_____
链接:https://www.jianshu.com/p/52197559b7c7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @   mingruqi  阅读(637)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2018-06-30 在PHP语言中使用JSON
2018-06-30 PHP中生成json信息的方法
2018-06-30 php发送get、post请求的6种方法简明总结
点击右上角即可分享
微信分享提示