
- iOS 教程
- iOS - 首頁
- iOS - 開始
- iOS - 環境設定
- iOS - Objective-C 基礎
- iOS - 第一個iPhone應用程式
- iOS - 動作和出口
- iOS - 代理
- iOS - UI 元素
- iOS - 加速計
- iOS - 通用應用程式
- iOS - 相機管理
- iOS - 位置處理
- iOS - SQLite 資料庫
- iOS - 傳送電子郵件
- iOS - 音訊和影片
- iOS - 檔案處理
- iOS - 訪問地圖
- iOS - 應用內購買
- iOS - iAd 整合
- iOS - GameKit
- iOS - 故事板
- iOS - 自動佈局
- iOS - Twitter 和 Facebook
- iOS - 記憶體管理
- iOS - 應用程式除錯
- iOS 有用資源
- iOS - 快速指南
- iOS - 有用資源
- iOS - 討論
iOS - 第一個iPhone應用程式
建立第一個應用程式
現在我們將建立一個簡單的單檢視應用程式(一個空白應用程式),它將在 iOS 模擬器上執行。
步驟如下。
步驟 1 - 開啟 Xcode 並選擇建立新的 Xcode 專案。

步驟 2 - 選擇單檢視應用程式。

步驟 3 - 輸入產品名稱(即應用程式名稱)、組織名稱,然後是公司識別符號。

步驟 4 - 確保選中使用自動引用計數,以便在資源超出作用域後自動釋放分配的資源。單擊下一步。
步驟 5 - 選擇專案的目錄並選擇建立。

步驟 6 - 您將看到如下螢幕 -

在上面的螢幕中,您可以選擇支援的方向、構建和釋出設定。有一個欄位部署目標,我們想要支援的裝置版本,讓我們選擇 4.3,這是現在允許的最小部署目標。目前,這些不是必需的,讓我們專注於執行應用程式。
步驟 7 - 現在,在“執行”按鈕附近的下拉選單中選擇 iPhone 模擬器,然後選擇執行。

步驟 8 - 就這樣;您已成功執行您的第一個應用程式。您將獲得如下輸出 -

現在讓我們更改背景顏色,以便開始使用介面構建器。選擇 ViewController.xib。在右側選擇背景選項,更改顏色並執行。

在上面的專案中,預設情況下,部署目標將設定為 iOS 6.0,並且將啟用自動佈局。為了確保我們的應用程式可以在 iOS 4.3 及更高版本的裝置上執行,我們已經在建立此應用程式的開始修改了部署目標,但我們沒有停用自動佈局。
要停用自動佈局,我們需要取消選中每個 nib(即 xib 檔案)的檔案檢查器中的自動佈局複選框。Xcode 專案 IDE 的各個部分如下圖所示(由 Apple Xcode 4 使用者文件提供)。

檔案檢查器位於上圖所示的檢查器選擇欄中,可以在那裡取消選中自動佈局。當您只想定位 iOS 6 裝置時,可以使用自動佈局。此外,如果您將部署目標提高到 iOS 6,您還可以使用許多新功能,例如 Passbook。目前,讓我們堅持使用 iOS 4.3 作為部署目標。
第一個 iOS 應用程式的程式碼
您會發現為您的應用程式生成的五個不同的檔案。它們列出如下 -
- AppDelegate.h
- AppDelegate.m
- ViewController.h
- ViewController.m
- ViewController.xib
AppDelegate.h
// 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
程式碼中的重要專案 -
AppDelegate 繼承自 UIResponder,用於處理 iOS 事件。
實現 UIApplicationDelegate 的委託方法,該方法提供關鍵的應用程式事件,例如完成啟動、即將終止等。
UIWindow 物件用於管理和協調 iOS 裝置螢幕上的各種檢視。它就像所有其他檢視載入的基礎檢視。通常,應用程式只有一個視窗。
UIViewController 用於處理螢幕流程。
AppDelegate.m
// Imports the class Appdelegate's interface import "AppDelegate.h" // Imports the viewcontroller to be loaded #import "ViewController.h" // Class definition starts here @implementation AppDelegate // Method to intimate us that 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 { /* 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:. */ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ } @end
程式碼中的重要專案 -
此處定義了 UIApplication 委託。上面定義的所有方法都是 UI 應用程式委託,不包含使用者定義的方法。
分配 UIWindow 物件以儲存已分配的應用程式。
UIViewController 分配為視窗的初始檢視控制器。
要使視窗可見,呼叫 makeKeyAndVisible 方法。
ViewController.h
#import <UIKit/UIKit.h> // Interface for class ViewController @interface ViewController : UIViewController @end
程式碼中的重要專案 -
ViewController 類繼承自 UIViewController,後者為 iOS 應用程式提供了基本的檢視管理模型。
ViewController.m
#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
程式碼中的重要專案 -
此處實現的兩種方法在基類 UIViewController 中定義。
在檢視載入後呼叫的 viewDidLoad 中進行初始設定。
在記憶體警告的情況下呼叫 didReceiveMemoryWarning 方法。