IOS開關的使用
開關用於打開和關閉狀態之間的切換。
重要的屬性
- onImage
- offImage
- on
重要的方法
- (void)setOn:(BOOL)on animated:(BOOL)animated
添加自定義方法 addSwitch 和開關
-(IBAction)switched:(id)sender{
NSLog(@"Switch current state %@", mySwitch.on ? @"On" : @"Off");
}
-(void)addSwitch{
mySwitch = [[UISwitch alloc] init];
[self.view addSubview:mySwitch];
mySwitch.center = CGPointMake(150, 200);
[mySwitch addTarget:self action:@selector(switched:)
forControlEvents:UIControlEventValueChanged];
}
在 ViewController.m 中修改 viewDidLoad,如下所示
(void)viewDidLoad
{
[super viewDidLoad];
[self addSwitch];
}
輸出
現在當我們運行該應用程式我們會看到下麵的輸出

向右滑動開關輸出如下所示

