本範例目的是使用按鈕(button)去改變開關(UISwitch)之狀態
1. storyboard中加入switch, button
2.與viewcontroller.h檔建立程式關聯性, 程式碼如下
@property (strong, nonatomic) IBOutletUISwitch *mySwitch;
- (IBAction)toClick:(id)sender;
3.點擊按鈕, 判斷switch狀態並切換, 程式碼如下
- (IBAction)toClick:(id)sender {
if(_mySwitch.on == YES)
{
[_mySwitch setOn:NO animated:YES];
}else{
[_mySwitch setOn:YES animated:YES];
}
}
viewcontroller.h程式碼內容
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutletUISwitch *mySwitch;
- (IBAction)toClick:(id)sender;
@end
viewcontroller.m程式碼內容
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (IBAction)toClick:(id)sender {
if(_mySwitch.on == YES)
{
[_mySwitch setOn:NO animated:YES];
}else{
[_mySwitch setOn:YES animated:YES];
}
}
@end
文章標籤
全站熱搜