您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 基礎知識 >> iOS 入門介紹

如何打造你的第一個 iPhone 應用程式

編輯:iOS 入門介紹

建立第一個 iPhone 應用程式

接下來我們實際做一個能在 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 時自然也無法使用。



深入了解第一個 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 物件負責管理螢幕上的多個檢視,就像其他被載入的檢視一樣;一般應用通常只有一個視窗。
  • 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 // 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
程式碼說明
  • 此處實作 UIApplication 相關邏輯,上述方法皆為系統生命週期回呼,不包含任何自訂方法。
  • 指派 UIWindow 物件以承載應用程式的畫面。
  • 將 ViewController 設為視窗的初始檢視控制器。
  • 呼叫 makeKeyAndVisible 讓視窗顯示出來。

ViewController.h

#import // 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 則在系統發出記憶體警告時呼叫。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved