iOS開發從入門到精通--開關按鈕UISwitch控制項

來源:互聯網
上載者:User

標籤:

開關按鈕UISwitch
在ViewController.h裡面

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    //定義一個開關控制項    //作用可以進行狀態的改變    //開,關:兩種狀態可以切換    //所有UIKit架構庫中的控制項均已UI開頭    //蘋果官方的控制項都定義在UIKit架構庫中    UISwitch * _mySwitch;}@property(retain,nonatomic) UISwitch * mySwitch;@end

在ViewController.m裡面

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize mySwitch=_mySwitch;- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //建立一個開關對象    //繼承於UIView的    _mySwitch = [[UISwitch alloc]init];    //蘋果官方的控制項的位置設定    //位置X,Y的值可以改變(100,100)    //寬度和高度值無法改變(80,40)寫了也沒有用的,不會起到作用的。預設的。    _mySwitch.frame=CGRectMake(100, 200, 180, 40);    //開關狀態設定屬性    //YES:開啟狀態    //NO:關閉狀態    _mySwitch.on=YES;    //也可以使用set函數    //[_mySwitch setOn:YES];    //設定開關狀態    //p1:狀態設定    //p2:是否開啟動畫效果    //[_mySwitch setOn:YES animated:YES];    [self.view addSubview:_mySwitch];    //設定開啟狀態的風格顏色    [_mySwitch setOnTintColor:[UIColor orangeColor]];    //設定開關圓按鈕的風格顏色    [_mySwitch setThumbTintColor:[UIColor blueColor]];    //設定整體風格顏色,按鈕的白色是整個父布局的背景顏色    [_mySwitch setTintColor:[UIColor greenColor]];    //向開關控制項添加事件函數    //p1:函數實現對象    //p2:函數對象    //p3:事件響應時的事件類型UIControlEventValueChanged狀態發生變化時觸發函數    [_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];}//參數傳入開關對象本身- (void) swChange:(UISwitch*) sw{    if(sw.on==YES){        NSLog(@"開關被開啟");    }else{        NSLog(@"開關被關閉");    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

iOS開發從入門到精通--開關按鈕UISwitch控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.