iOS 教程
現(xiàn)在讓我們來創(chuàng)建一個(gè)在iOS模擬器上運(yùn)行的簡(jiǎn)單視圖應(yīng)用(空白的應(yīng)用程序)。
操作步驟如下:
1、打開Xcode并選擇創(chuàng)建一個(gè)新的Xcode項(xiàng)目。
2. 然后選擇單一視圖應(yīng)用程序
3. 接下來輸入產(chǎn)品名稱即應(yīng)用程序名稱、組織名稱和公司標(biāo)識(shí)符。
4. 確定已經(jīng)選擇自動(dòng)應(yīng)用計(jì)數(shù),以自動(dòng)釋放超出范圍的資源。單擊下一步。
5.選擇項(xiàng)目目錄并選擇創(chuàng)建
6. 你將看到如下所示的頁(yè)面
屏幕上方能夠設(shè)置方向、生成和釋放。有一個(gè)部署目標(biāo),設(shè)備支持4.3及以上版本的部署目標(biāo),這些不是必須的,現(xiàn)在只要專注于運(yùn)行該應(yīng)用程序。
7. 在下拉菜單中選擇iPhone Simulator并運(yùn)行。
8. 成功運(yùn)行第一個(gè)應(yīng)用程序,將得到的輸出,如下所示。
更改背景顏色使之有開始的界面生成器。選擇ViewController.xib。在右側(cè)選擇背景選項(xiàng),更改顏色并運(yùn)行。
在上述項(xiàng)目中,默認(rèn)情況下,部署目標(biāo)已設(shè)置為iOS6.0且自動(dòng)布局將被啟用。
為確保應(yīng)用程序能iOS4.3設(shè)備上正常運(yùn)行,我們已經(jīng)在開始創(chuàng)建應(yīng)用程序時(shí)修改了部署目標(biāo),但我們不禁用自動(dòng)布局,要取消自動(dòng)布局,我們需要取消選擇自動(dòng)班上復(fù)選框在文件查看器的每個(gè)nib,也就是xib文件。
Xcode項(xiàng)目IDE的各部分顯示如下(蘋果Xcode4用戶文檔)
在上面所示的檢查器選擇器欄中可以找到文件檢查器,且可以取消選擇自動(dòng)布局。當(dāng)你想要的目標(biāo)只有iOS6.0的設(shè)備時(shí),可以使用自動(dòng)布局。
當(dāng)然,也可以使用新功能,如當(dāng)加注到iOS6時(shí),就可以使用passbook這一功能。現(xiàn)在,以Ios4.3作為部署目標(biāo)。
我們使用單行注釋(//)來解釋簡(jiǎn)單代碼,重要的項(xiàng)目代碼解釋在代碼下方。
// 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
// 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
#import // Interface for class ViewController @interface ViewController : UIViewController @end
#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