iOS - 建立第一個iPhone應用


建立第一個iPhone應用

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

步驟如下。

1. 開啟Xcode和選擇建立一個新的Xcode專案。

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

3. 然後輸入專案名稱,即應用程式的名稱,組織名稱和公司標識

4. 確保使用自動參照計數的選擇,以便自動釋放分配的資源,一旦超出範圍。單擊 Next.

5. 選擇專案的目錄,並選擇create.

6. 你會看到如下的一個螢幕

在螢幕上方你將能夠選擇支援的方向,建立和釋放設定。現場部署有一個目標,我們要支援裝置版本,選擇4.3這是現在最小允許部署目標。現在這些都不是必需的,讓我們把注意力集中在執行的應用程式。

7. 現在選擇iPhone模擬器在附近執行按鈕的下拉,選擇“run”。 

8. 已經成功地執行第一個應用程式。會得到一個輸出如下

現在讓我們來改變背景顏色,只是為了有一個開始介面生成器。選擇ViewController.xib。選擇“background ”選項,在右側,改變顏色並執行。

在上述專案中,預設情況下部署目標已設定到 iOS6.0,自動布局將啟用。但是,為了確保我們的應用程式執行的裝置上執行的iOS 4.3開始,我們已經修改了部署目標在建立這個應用程式的開始,但我們沒有禁用自動布局,禁用自動布局,我們需要取消選擇“自動布局xib檔案的每個 nib,即在 inspector 核取方塊。 Xcode專案IDE的各個部分是下圖中(註:蘋果 Xcode4 使用者文件)。

檔案檢查器處於找到檢查器選擇欄中,如上圖所示,自動布局可以是取消選中。當想只針對的iOS6裝置,可用於自動布局。此外,將能夠使用許多新功能,如果提高到iOS6部署目標。這裡使用 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委託的委託方法提供關鍵的應用程式事件等成品發起,終止等。

  • 一個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 


// Following method intimates us the application launched  successfully 
- (BOOL)application:(UIApplication *)application 
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    self.window = [[UIWindow alloc] initWithFrame:
	[[UIScreen mainScreen] bounds]]; 
    // Override yiibai 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中分配物件來儲存應用程式被分配

  • UIViewController的分配視窗的初始檢視控制器。

  • 為了使視窗的可見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方法被呼叫時記憶體不足的情況下發出警告。