【iOS 初見】第一個簡單的 iOS 應用,ios初見
本執行個體來自 《iOS編程(第4版)》,介紹如何編寫一個簡單的 iOS 應用。
功能為:在視圖中顯示一個問題,使用者點擊視圖下方的按鈕,可以顯示相應的答案,使用者點擊上方的按鈕,則會顯示一個新的問題。
步驟如下:
1.建立一個新的Xcode項目 Hello_iOS,具體看:
2.建立一個視圖控制器類檔案 QAViewController ,注意看:
3.選取QAViewController.xib 檔案,從物件程式庫中拖拽4個Label 、2個 Button 如擺放。
4.為對象建立關聯。首先,修改QAViewController.m 檔案如下:
#import "QAViewController.h"@interface QAViewController ()//聲明插座變數@property (nonatomic,weak) IBOutlet UILabel *questionLabel;@property (nonatomic,weak) IBOutlet UILabel *answerLabel;@end@implementation QAViewController@end
5.設定聲明的插座變數,如所示:
6.重新開啟QAViewController.m 檔案,聲明兩個按鈕的動作方法:
@implementation QAViewController-(IBAction)showQuestion:(id)sender{ }-(IBAction)showAnswer:(id)sender{ }@end
7.接著要關聯聲明的動作方法,設定目標和動作(按住Control並拖拽或按住右鍵拖拽至File's Owner):
8.以上完成了此應用的XIB檔案,建立並設定了應用所需的視圖對象,並為視圖對象和控制器對象建立了所有必需的管理。下面就需要開始建立模型對象。在項目導航面板中選擇 QAViewController.m 檔案,修改代碼如下:
//// QAViewController.m// Hello_iOS//// Created by YeChao on 15/12/5.// Copyright (c) 2015年 Luka.Ye. All rights reserved.//#import "QAViewController.h"@interface QAViewController ()//聲明插座變數@property (nonatomic,weak) IBOutlet UILabel *questionLabel;@property (nonatomic,weak) IBOutlet UILabel *answerLabel;//聲明一個整形對象和兩個數組對象//整形變數用於跟蹤使用者正在回答的問題@property (nonatomic) int currentQuestionIndex;//兩個數組用於儲存一系列問題和答案@property (nonatomic,copy) NSArray *questions;@property (nonatomic,copy) NSArray *answers;@end@implementation QAViewController-(instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ //調用父類實現的初始化方法 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { //建立兩個數組對象,儲存所需的問題和答案 //同時,將 questions和 answers分別指向問題數組和答案數組 self.questions = @[@"你叫什麼名字?",@"你覺得你帥嗎?",@"今年是哪一年?",@"明明可以靠臉吃飯,為何要靠才華?"]; self.answers = @[@"可以叫我葉小超。",@"還行吧。",@"公元2015年",@"這是和明明的差距,要努力啊。"]; } //返回新對象的地址 return self;}-(IBAction)showQuestion:(id)sender{ //進入下一個問題 self.currentQuestionIndex++; //是否已經回答完了所有問題? if (self.currentQuestionIndex==[self.questions count]) { //回到第一個問題 self.currentQuestionIndex =0; } //根據正在回答的問題序號從數組中取出問題字串 NSString *question = self.questions[self.currentQuestionIndex]; //將問題字串顯示在標籤上 self.questionLabel.text = question; //重設答案字串 self.answerLabel.text = @""; }-(IBAction)showAnswer:(id)sender{ //當前問題的答案是什嗎? NSString *answer = self.answers[self.currentQuestionIndex]; //在答案標籤上顯示相應的答案 self.answerLabel.text = answer;}@end
9.如果現在運行項目,將只能看到一個空白的螢幕,無法看到在 QAViewController.xib 檔案中建立的使用者介面。為了在螢幕上顯示使用者介面,必須將視圖控制器和應用中的另一個控制器關聯——AppDelegate 。
在項目導航面板中選擇 AppDelegate.m 檔案。在 application didFinishLaunchingWithOptions:方法中建立 QAViewController 對象,並將它設定為 UIWindow 對象的根視圖控制器。
#import "AppDelegate.h"#import "QAViewController.h"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //在這裡添加應用啟動後的初始化代碼 QAViewController *qaVC = [[QAViewController alloc] init]; self.window.rootViewController = qaVC; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}
10.按住 command+R 運行項目,效果如下:
11.沒有設定應用表徵圖的話,會是一塊白板。應用表徵圖是一張圖片,用於在主畫面上指代應用。不同裝置對表徵圖的尺寸要求也不同。
12.設定應用表徵圖:選中項目導航面板中的 Images.xcassets 條目,如從Finder拖拽至AppIcon地區的設定塊上。
13.重新運行項目就可以了。