博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对比学习UIKit和AppKit--入门级
阅读量:7070 次
发布时间:2019-06-28

本文共 3238 字,大约阅读时间需要 10 分钟。

UIKit是用来开发iOS的应用的,AppKit是用来开发Mac应用的,在使用过程中他们很相似,可是又有很多不同之处,通过对比分析它们的几个核心对象,可以避免混淆。

UIKit和AppKit都有一个Application类,每个应用都只创建一个Application对象,分别是UIAplication和NSApplication的实例。但是创建这个对象的方式还是稍有不同,看iOS应用的main函数:

int main(int argc, char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));    }}

 再看Mac应用的main函数:

int main(int argc, const char * argv[]){    return NSApplicationMain(argc, argv);}

 UIApplicationMain

This function instantiates the application object from the principal class and instantiates the delegate (if any) from the given class and sets the delegate for the application. It also sets up the main event loop, including the application’s run loop, and begins processing events. If the application’s Info.plist file specifies a main nib file to be loaded, by including the NSMainNibFile key and a valid nib file name for the value, this function loads that nib file.

NSApplicationMain

Creates the application, loads the main nib file from the application’s main bundle, and runs the application. You must call this function from the main thread of your application, and you typically call it only once from your application’s main function, which is usually generated automatically by Xcode.

概括来说,主要都是创建Application对象,set up event loop,并开始处理event。区别是iOS上可以提供自定义的UIApplication,Application delegate是在main函数中指定的。

而Mac上Application Delegate是在nib/xib 文件中指定的,而且NSApplicationMain会读取Info.plist,得到main xib文件,并加载进来,如果main xib信息不存在或者不正确,程序就无法运行。相对应的,iOS应用使用Storyboard,iOS应用可以指定一个Main storyboard,然后由UIApplicationMain自动加载,但是这不是必须,如果不指定,程序也可以启动,如果什么都不做,就显示黑屏,但Xcode会为Empty Application手动创建Window对象,这样启动后就显示空白了,但如果指定就要指定正确,否则就会无法启动了。如果不在Info.plist中指定,依然可以在Application Delegate 的 - (void)applicationDidFinishLaunching:(NSNotification *)notification; 中设置,比如下面的例子:

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

上面这段代码手动创建Window对象,并创建root UIViewController,这样也是可以正常显示的。

下面再说说Window对象。在Mac和iOS上,一个Application都是可以创建多个Window对象的。iOS上在任一时刻只有一个key window,key window就是最后一个被发送了makeKeyAndVisible的Window。Mac上一个Application有多个Window是很容易理解的,iOS上的多个Window是怎样工作的呢?对于大多数iOS的应用而言,都只会创建一个Window,但如果应用需要在另外一个screen上显示内容,那就可以创建一个Window。即使没有external display,iOS也是支持在一个Application中创建多个Window,只是我们的程序需要控制如何切换它们就可以了。

关于Window,在iOS中还发现一个有意思的事情,AppDelegate中实现window property时用到了auto-synthesis,auto-synthesis就是只写一个@property,编译器就自动声称@synthesize的代码,但是如果一个类conform to一个protocol,而protocol中声明了一个property,auto-synthesis 并不会自动声称@synthesize的代码,这时加一个@property或者@synthesize就可以了。

 

下面看看Window和View以及ViewController的关系。不过就不在这篇里写了。

 

Reference:

1. https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingWindows/CreatingWindows.html

转载于:https://www.cnblogs.com/whyandinside/p/3706494.html

你可能感兴趣的文章
HPE牵手DDN打造整合的高性能服务器存储产品组合
查看>>
mycat分片规则之范围约定规则(auto-sharding-long)
查看>>
windows配置java环境变量
查看>>
python流程处理
查看>>
<kubernetes in action>看书笔记
查看>>
python密码破解工具patator
查看>>
众筹网站Kickstarter不准备上市:转型公益企业
查看>>
OpenStack入门修炼之nova服务(计算节点)的部署与测试(11)
查看>>
ubuntu安装apache php mysql phpmyadmin
查看>>
漫画: DBA和小D的日常
查看>>
代码 实现UIDatePicker控件 和 Tab Bar 视图切换
查看>>
USB引导盘制作
查看>>
通过自己的项目实际经验,阐述为什么“恶心玩技术”?玩Java技术的教训(一)...
查看>>
蚂蚁分类信息系统 5.8 地区增加多级分类
查看>>
Spring-web源码解析之ContextLoaderListener
查看>>
设计模式--六大原则
查看>>
MONyog监控系统
查看>>
后缀数组 --- HDU 3518 Boring counting
查看>>
让你的虚拟机飞起来--VMware workstaion
查看>>
Yeslab 马老师 V2V环境下vCenter Server Heartbeat v6.4实现vCenter5.0的双机备份
查看>>