現(xiàn)在,我們只是要創(chuàng)建一個簡單的單視圖的應(yīng)用程序(一個空白的應(yīng)用程序),只運行在iOS模擬器。
步驟如下。
1. 打開Xcode和選擇創(chuàng)建一個新的Xcode項目。
2. 然后選擇單一視圖應(yīng)用
3. 然后輸入項目名稱,即應(yīng)用程序的名稱,組織名稱和公司標(biāo)識
4. 確保使用自動引用計數(shù)的選擇,以便自動釋放分配的資源,一旦超出范圍。單擊 Next.
5. 選擇項目的目錄,并選擇create.
6. 你會看到如下的一個屏幕
在屏幕上方你將能夠選擇支持的方向,建立和釋放設(shè)置?,F(xiàn)場部署有一個目標(biāo),我們要支持設(shè)備版本,選擇4.3這是現(xiàn)在最小允許部署目標(biāo)?,F(xiàn)在這些都不是必需的,讓我們把注意力集中在運行的應(yīng)用程序。
7. 現(xiàn)在選擇iPhone模擬器在附近運行按鈕的下拉,選擇“run”。
8. 已經(jīng)成功地運行第一個應(yīng)用程序。會得到一個輸出如下
現(xiàn)在讓我們來改變背景顏色,只是為了有一個開始界面生成器。選擇ViewController.xib。選擇“background ”選項,在右側(cè),改變顏色并運行。
在上述項目中,默認(rèn)情況下部署目標(biāo)已設(shè)定到 iOS6.0,自動布局將啟用。但是,為了確保我們的應(yīng)用程序運行的設(shè)備上運行的iOS 4.3開始,我們已經(jīng)修改了部署目標(biāo)在創(chuàng)建這個應(yīng)用程序的開始,但我們沒有禁用自動布局,禁用自動布局,我們需要取消選擇“自動布局xib文件的每個 nib,即在 inspector 復(fù)選框。 Xcode項目IDE的各個部分是下圖中(注:蘋果 Xcode4 用戶文檔)。
文件檢查器處于找到檢查器選擇欄中,如上圖所示,自動布局可以是取消選中。當(dāng)想只針對的iOS6設(shè)備,可用于自動布局。此外,將能夠使用許多新功能,如果提高到iOS6部署目標(biāo)。這里使用 iOS4.3 部署目標(biāo)。
會發(fā)現(xiàn)5個不同的文件,將已生成的應(yīng)用程序,如下。
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController.xib
我們使用這些單行注釋(/ /)給下面的代碼解釋簡單的代碼解釋和重要的項目。
// 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事件
實現(xiàn) UIApplication委托的委托方法提供關(guān)鍵的應(yīng)用程序事件等成品發(fā)起,終止等。
一個UIWindow對象來管理和協(xié)調(diào)各方面的意見在iOS設(shè)備上屏幕。這就像所有其他加載意見的基礎(chǔ)視圖。在一般情況下只有一個應(yīng)用程序的一個窗口。
UIViewController 處理??畫面流暢。
// 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定義的委托。上述定義的所有方法是用戶界面應(yīng)用程序代表和不包含任何用戶定義的方法。
UIWindow中分配對象來保存應(yīng)用程序被分配
UIViewController的分配窗口的初始視圖控制器。
為了使窗口的可見makeKeyAndVisible方法被調(diào)用。
#import// Interface for class ViewController @interface ViewController : UIViewController @end
ViewController 類繼承的UIViewController為iOS應(yīng)用程序提供了基本的視圖管理模式。
#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 didReceiveMemory