接下來我們實際做一個能在 iOS 模擬器上跑起來的簡易 View-based Application(空白應用)。
操作步驟如下:
1、開啟 Xcode,選擇建立新的專案。

2、選擇 Single View Application。

3、填入產品名稱(即應用名稱)、組織名稱與公司識別碼。

4、確認已勾選「使用自動引用計數」,讓超出範圍的物件自動釋放,然後點下一步。
5、選擇專案存放目錄並點擊建立。

6、接著會看到專案主畫面。

上方可以設定 orientation、Target 與 Deployment Target 等項目。這些目前都不是重點,先專注把程式跑起來。
7、從執行目標的下拉選單中選擇 iPhone Simulator,點擊執行。

8、成功執行後,就會看到模擬器上的畫面。

想改背景色,可以從介面建置器下手:選擇 ViewController.xib,在右側找到背景選項,換個顏色再執行一次。

此專案預設 Deployment Target 為 iOS 6.0 且啟用自動布局。若希望同時支援 iOS 4.3 裝置,建立專案時就要調整部署目標;但由於自動布局不為舊版所支援,還需逐一在各個 xib 檔中取消勾選「使用自動布局」。
Xcode 的各個區域如下圖所示(依 Apple Xcode 4 使用者文件)。

如上圖的檢查器選項列所示,在 File Inspector 中即可取消自動布局。若你的目標裝置僅為 iOS 6.0,那麼放心使用自動布局;同理,像 Passbook 這類僅 iOS 6 才有的功能,在部署目標為 iOS 4.3 時自然也無法使用。
專案共由五個檔案組成:
以下用單行註解(//)標示說明,重要段落則在程式碼下方補充。
// Header File that provides all UI related items. #import <UIKit/UIKit.h> // Forward declaration (Used when class will be defined /imported in future) @class ViewController; // Interface for Appdelegate @interface AppDelegate : UIResponder <UIApplicationDelegate> // Property window @property (strong, nonatomic) UIWindow *window; // Property Viewcontroller @property (strong, nonatomic) ViewController *viewController; //this marks end of interface @end
// Imports the class Appdelegate's interface import "AppDelegate.h" // Imports the viewcontroller to be loaded #import "ViewController.h" // Class definition starts here @implementation AppDelegate // Following method intimates us the application launched successfully - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.*/ } - (void)applicationDidEnterBackground:(UIApplication *)application { /* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.*/ } - (void)applicationWillEnterForeground:(UIApplication *)application { /* Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.*/ } - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ } @end
#import // Interface for class ViewController @interface ViewController : UIViewController @end
#import "ViewController.h" // Category, an extension of ViewController class @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end