建立第一款iPhone應用程式

現在讓我們來建立一個在iOS模擬器上執行的簡單檢視應用(空白的應用程式)。

操作步驟如下:

1、開啟Xcode並選擇建立一個新的Xcode專案。

XcodeWelcomePage

2. 然後選擇單一檢視應用程式

CreateProject

3. 接下來輸入產品名稱即應用程式名稱、組織名稱和公司識別符號。

NewProjectCreateOptions

4. 確定已經選擇自動應用計數,以自動釋放超出範圍的資源。單擊下一步。

5.選擇專案目錄並選擇建立

CreateProjectSelectFolder

6. 你將看到如下所示的頁面

XcodeProjectPage

螢幕上方能夠設定方向、生成和釋放。有一個部署目標,裝置支援4.3及以上版本的部署目標,這些不是必須的,現在只要專注於執行該應用程式。

7. 在下拉選單中選擇iPhone Simulator並執行。

runProject

8. 成功執行第一個應用程式,將得到的輸出,如下所示。

iPhoneSimulator1

更改背景顏色使之有開始的介面生成器。選擇ViewController.xib。在右側選擇背景選項,更改顏色並執行。

InterfaceBuilder1

在上述專案中,預設情況下,部署目標已設定為iOS6.0且自動佈局將被啟用。

為確保應用程式能iOS4.3裝置上正常執行,我們已經在開始建立應用程式時修改了部署目標,但我們不禁用自動佈局,要取消自動佈局,我們需要取消選擇自動班上覆選框在檔案檢視器的每個nib,也就是xib檔案。

Xcode專案IDE的各部分顯示如下(蘋果Xcode4使用者文件)

Xcode4Workspace

在上面所示的檢查器選擇器欄中可以找到檔案檢查器,且可以取消選擇自動佈局。當你想要的目標只有iOS6.0的裝置時,可以使用自動佈局。

當然,也可以使用新功能,如當加註到iOS6時,就可以使用passbook這一功能。現在,以Ios4.3作為部署目標。



深入瞭解第一款IOS應用程式程式碼

5個不同檔案生成應用程式,如下所示
  • 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事件。
  • 完成UIApplication的命令,提供關鍵應用程式事件,如啟動完畢,終止,等等
  • 在iOS裝置的螢幕上用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。上面定義的所有方法都是應用程式UI調動和不包含任何使用者定義的方法。
  • UIWindow物件被分配用來儲存應用程式分配物件。
  • UIController作為視窗初始檢視控制器
  • 呼叫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中的安裝程式
  • 在記憶體警告的情況下呼叫didReceviveMemoryWarning