iOS菜鳥成長筆記(1)——第一個iOS應用

前言:陽光小強最近抽時間學習iOS開發,在學習過程中發現了很多有趣的東西也遇到了很多問題,爲了在學習過程中能和大家交流,記錄下學習的心得和學習成果,所以就有了這一個系列文章,希望這一系列文章能形成一個系統性的東西,讓和我一樣剛步入iOS開發的朋友少走彎路,用最少的時間獲得最大的收益。既然是學習筆記,希望大家多提意見,如果你是iOS大牛多多拍磚。

說起iOS開發很多朋友就會望而卻步,有一部分朋友可能是因爲設備因素,有一部分朋友可能是因爲編程語言是Objective-C的原因,因爲這些遲遲沒有拿起的iOS當你有一天進入它的世界,你就會發現其實是我們想多了,在Xcode上面開發iOS程序是非常方便快捷的,而且蘋果開發者官網爲我們提供了很多關於iOS開發的文檔和文章,學習起來非常方便。

轉載請說明出處:http://blog.csdn.net/dawanganban

一、着手開發IOS應用程序

(官網鏈接:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#//apple_ref/doc/uid/TP40012668)


正如上圖一樣,這篇文章(https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#//apple_ref/doc/uid/TP40012668)介紹了iOS開發工具Xcode的基本使用和開發的基本步驟和過程,建議都能讀一下。

iOS開發中使用的是MVC模型,這個對於做過java ee和Android的朋友來說已經很熟悉了,我們需要做的是把模型、視圖、控制器和工程中的文件對應起來


下面我們先來新建一個iOS的項目

1、選擇 Create a new Xcode project --> iOS --> Application --> Single View Application (選擇單頁面模板)


2、項目結構


如上圖所示,我們先來看看 Supporting Files/main.m文件,這個大家一看就知道是整個應用的入口

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
可以看到當我們的iOS應用運行的時候首先會執行UIApplication的UIApplicationMain方法,這個方法首先會創建UIApplication實例(這個和Android中的Application類似,是一個單例模式,整個應用程序只有一個實例,所以它的生命週期和我們的應用生命週期一致),接下來會在這裏循環管理和處理應用事件。同時也會創建一個UIApplicationDelegate類的實例。該類是UIApplication的代理類,在該類中處理UIApplication委託的各種事件響應。

下面我們打開工程中的AppDelegate.h和AppDelegate.m文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
可以看到改代理類持有一個UIWindow的實例(屬性)

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    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
在實現中有6個方法,分別在應用程序的各個生命週期中調用。比如didFinishLaunchingWithOptions方法在應用程序加載完成後調用。

工程中的ViewController就是MVC中的控制器,視圖就是screen.xib文件和storyboard中的界面。這裏的ViewController繼承自UIViewController相當於Android中的Activity,用來控制視圖和模型的交互。


視圖控制器並不是視圖層的一部分,也不是界面中的元素,他管理者視圖層的對象,並提供交互行爲,如果有多個界面,我們就要爲每一個界面定義一個視圖控制器。

摘自官方文檔 : “您還可以使用視圖控制器來轉換各種類型的內容。由於 iOS 應用程序顯示內容的空間很有限,因此視圖控制器提供了所需要的基礎結構,可讓您移除一個視圖控制器的視圖,替換爲另一個視圖控制器中的視圖。通過讓視圖控制器文件與串聯圖中的視圖進行通信,可以定義應用程序中的交互方式。方法是通過 Action 與 Outlet 來定義串聯圖與源代碼文件之間的連接。” 

下面通過一個簡單的實例,我們就可以看到Action和Outlet的使用了。

二、拖放UI組件

打開Main.storyboard向其中拖放組件元素。


上面用到了 UITextField、UILabel、UIButton,有關這些組件的詳細配置我覺得對於做過Android開發的朋友真心沒必要花過多時間。

三、監聽Button事件

ViewController.h  (第6行代碼)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//聲明一個方法來監聽按鈕點擊 IBAction === void
- (IBAction)btnOnClick;

@end
ViewController.m  (第15-17行代碼)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark 監聽按鈕點擊
- (void)btnOnClick{
    NSLog(@"按鈕被人點擊了");
}

@end

按住control鍵,拖動Button到右邊的 -(IBActon)btnOnClick處建立連接。

command+R運行觀察結果,點擊按鈕會發現控制檯打印出了Log

2015-03-14 11:10:11.820 第一個iOS程序[12810:142497] 按鈕被人點擊了
2015-03-14 11:10:12.004 第一個iOS程序[12810:142497] 按鈕被人點擊了

四、獲取文本框對象

我們要獲取文本對象,先要將文本對象作爲ViewController的一個屬性(第9、10行)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//聲明一個方法來監聽按鈕點擊 IBAction === void
- (IBAction)btnOnClick;

//聲明兩個屬性用來保存2個文本輸入框
@property (nonatomic, weak) IBOutlet UITextField *num1;
@property (nonatomic, weak) IBOutlet UITextField *num2;

@end
用同樣的方法(按住control鍵拖動我們的文本對象建立連接),這樣我們就可以取得文本對象的內容了

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark 監聽按鈕點擊
- (void)btnOnClick{
    NSLog(@"按鈕被人點擊了");
    
    UITextField *textField1 = self.num1;
    UITextField *textField2 = self.num2;
    
    NSString *text1 = textField1.text;
    NSString *text2 = textField2.text;
    
    NSLog(@"文本1 = %@, 文本2 = %@", text1, text2);
    
}

@end
command + R ,在文本框內輸入後點擊按鈕

控制檯輸出:

2015-03-14 11:33:07.342 第一個iOS程序[14531:166271] 按鈕被人點擊了
2015-03-14 11:33:07.343 第一個iOS程序[14531:166271] 文本1 = 12, 文本2 = 34

五、完成計算功能

用同樣的方法添加UILabel的屬性

#pragma mark 監聽按鈕點擊
- (void)btnOnClick{
    NSLog(@"按鈕被人點擊了");
    
    UITextField *textField1 = self.num1;
    UITextField *textField2 = self.num2;
    
    NSString *text1 = textField1.text;
    NSString *text2 = textField2.text;
    
    NSLog(@"文本1 = %@, 文本2 = %@", text1, text2);
    
    double num1 = [text1 doubleValue];
    double num2 = [text2 doubleValue];
    
    double sum = num1 + num2;
    
    NSLog(@"文本和 = %f", sum);
    
    UILabel *resultLabel = self.result;
    
    resultLabel.text = [NSString stringWithFormat:@"%f", sum];
    
}

我們會發現輸出結果後面是三個點,這個是因爲我設置了長度超出省略,可以設置Line Breaks 爲 Clip(截斷)

最後再提一句:Objective-C是iOS的基石(核心)所以希望深入研究iOS開發的朋友建議加深對OC的學習,可以參考我的另外一個系列博客《Objective-C基礎筆記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章