iOS - 創建第一個iPhone應用

創建第一個iPhone應用

現在,我們只是要創建一個簡單的單視圖的應用程序(一個空白的應用程序),只運行在iOS模擬器。

步驟如下。

1. 打開Xcode和選擇創建一個新的Xcode項目。

iOS

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

iOS

3. 然後輸入項目名稱,即應用程序的名稱,組織名稱和公司標識

iOS

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

5. 選擇項目的目錄,並選擇create.

iOS

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

iOS

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

7. 現在選擇iPhone模擬器在附近運行按鈕的下拉,選擇「run」。 

iOS

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

iOS

現在讓我們來改變背景顏色,只是爲了有一個開始界面生成器。選擇ViewController.xib。選擇「background 」選項,在右側,改變顏色並運行。

iOS

在上述項目中,默認情況下部署目標已設定到 iOS6.0,自動佈局將啓用。但是,爲了確保我們的應用程序運行的設備上運行的iOS 4.3開始,我們已經修改了部署目標在創建這個應用程序的開始,但我們沒有禁用自動佈局,禁用自動佈局,我們需要取消選擇「自動佈局xib文件的每個 nib,即在 inspector 複選框。 Xcode項目IDE的各個部分是下圖中(注:蘋果 Xcode4 用戶文檔)。

iOS

文件檢查器處於找到檢查器選擇欄中,如上圖所示,自動佈局可以是取消選中。當想只針對的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方法被調用時內存不足的情況下發出警告。