对比学习UIKit和AppKit--入门级

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

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

1
2
3
4
5
6
int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

 再看Mac应用的main函数:

1
2
3
4
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; 中设置,比如下面的例子:

1
2
3
4
5
6
7
8
9
10
11
- (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是怎样工作的呢?阅读https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingWindows/CreatingWindows.html

 

下面看看Window和View以及ViewController的关系。

UIWindow只需要一个rootViewController,

 

 

 

 

对比学习UIKit和AppKit--入门级,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。