星期三, 5月 28, 2014

[iOS] 快快樂樂寫iOS hello world (storyboards)

萬事起頭難XD克服第一個helloworld iOS程式。
這個範例是參考 http://ios-blog.co.uk/tutorials/ios-7-hello-world-tutorial/
不過有改了一些操作方法:D
另一篇則有簡單的Xcode IDE的介紹:http://codewithchris.com/hello-world-iphone-app/

版本

XCode 5 

題目

透過二個按鈕來操作count次數,並將count的結果顯示在label上




步驟


Step1: 開啟新專案


File->New->Project




Step2: 選擇Single View Application專案類型




Step3: 填寫專案資訊


Product Name and Class Prefix各填入counter,公司資訊可以是任何值
確定後,Xcode會在專案內部建立以下五個檔案

1) CounterAppDelegate.h
2) CounterAppDelegate.m
3) MainStoryboard.storyboard (配置layout)
4) CounterViewController.h (宣告變數的檔案)
5) CounterViewController.m (實作方法的檔案)

Step4: 配置storyboard

點一下MainStoryboard.storyboard 打開配置的介面,
在畫面右下角有一個Object Library區塊,放了很多元件,



請拉二個按鈕跟一個Label


Step5: 宣告label變數與按鈕的action事件 - CounterViewController.h 
@interface CounterViewController : UIViewController

//變數都要放在{}裡面
{
    IBOutlet UILabel *lbl;
    int count;
}

//每個元件的動作function
- (IBAction)decrement:(id)sender;
- (IBAction)increment:(id)sender;

@end



Step6: 建立元件的連結(connection)

接著需要將按鈕的動作(action)與標籤的顯示(outlets)

該文是透過Assistant editor,如下圖。

不過同事建議使用左邊的第一個icon,Standard editor。

1) 點選按鈕
2) 點選Touch Up Inside,按住mouse左鍵,拖拉到View Controller建立關聯(顯示藍線),建立成功的話會從原本的空心圓變實心圓!!

Tip:

  • 這個Utlities右側欄是由這個控制(hide or show),
  • Sent Events這個東西需點選這個 




3) 選擇要綁定的變數

將-1button綁定 decrement

同理來綁定Label,注意這裡是選Referencing Outlets


Step7: 撰寫執行程式 - CounterViewController.m 

- (IBAction)decrement:(id)sender {
    lbl.text = [NSString stringWithFormat:@"%d", --count];
}
- (IBAction)increment:(id)sender {
    
    lbl.text = [NSString stringWithFormat:@"%d", ++count];
}

Step8: 執行程式

Product->Run



參考資料
http://ios-blog.co.uk/tutorials/ios-7-hello-world-tutorial/
http://codewithchris.com/hello-world-iphone-app/
http://www.techotopia.com/index.php/Creating_a_Simple_iOS_7_App

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails