2012年10月5日 星期五

Switch的基本使用(ㄧ)


一個最基本的Switch的設定與使用
Xcode版本4.5 & IOS6


1. 開啓專案


2. 設定StoryBoard,引進一個UISwitch及一個Label,並設定大小與顏色。

3. 註冊元件到switchViewController.h
#import <UIKit/UIKit.h>

@interface switchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

@end

4. 加入控制碼到switchViewController.m
#import "switchViewController.h"

@interface switchViewController ()

@end

@implementation switchViewController

@synthesize showLabel;
@synthesize mySwitch;

- (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.
}

- (IBAction)switchChange:(id)sender {
   
    if (mySwitch.on == 1){
        showLabel.text = @"開啟";
    }
    else {
        showLabel.text = @"關閉";
    }

}
@end

5. 執行結果


沒有留言: