2012年8月21日 星期二

Button與Label的聯結(一)

一個自己實驗的範例包含靜態與動態形式
Xcode版本4.4.1

Button基本Event說明
Touch Up Inside: 碰觸離手
Touch Drag Inside: 碰觸拖拉

一些初始細節也可參考:http://www.guomii.com/posts/20637

1. 首先開始一個新專案


 

 2. 將專案名字定為ButtonTest(你也可以改成自己想要的名稱),基本檔名完成後如下圖






3.此時會見到一個空白的storyboard,將右邊的Label及Button拖拉到中間,在Label及Button的中間按兩下就可以打字了


4. 將Xcode改成並行編輯模式,注意圖的右上的Editor的中間給他按下去,再分別選擇storyboard及ButtonTestViewController.h
不清楚可參考這裡:http://www.apple.com.cn/developer/technologies/tools/whats-new.html
然後將Label及Button分別從storyboard拖到ButtonTestViewController.h形成Outlet的property,我分別設定名稱為helloButton及helloLabel


#import <UIKit/UIKit.h>

@interface ButtonTestViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *helloButton;
@property (weak, nonatomic) IBOutlet UILabel *helloLabel;

- (void)setButtonInterface;

@end





 5. 再將Editor的右邊換成ButtonTestViewController.m,然後將Button再拖一次到右邊程式碼區,我設定名稱為onHelloButton

#import "ButtonTestViewController.h"

@interface ButtonTestViewController ()


@end

@implementation ButtonTestViewController
@synthesize helloButton; //根據ButtonTestViewController.h的property自動設定
@synthesize helloLabel;  //根據ButtonTestViewController.h的property自動設定

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

- (void)viewDidUnload
{
    [self setHelloButton:nil];
    [self setHelloLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (IBAction)onHelloButoon:(id)sender {
   
    helloLabel.text = @"Hello Button";
}



6. 到此基本的靜態設定已經完成,可以直接執行Run,就可以得到如下的執行結果



按下前

按下後



















沒有留言: