對比學習UIKit和AppKit--入門級


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM