ios3--UIView的常見方法

來源:互聯網
上載者:User

標籤:efault   return   ios   self   ati   from   哈哈   views   int   

////  ViewController.m//  07-UIView的常見方法//#import "ViewController.h"@interface ViewController ()// 紅色的view,通過連線,連線就是把oc代碼和UI綁定了。@property (weak, nonatomic) IBOutlet UIView *redView;/** 紅色的view  */@property (nonatomic, weak) UIView *redView1;@property (weak, nonatomic) IBOutlet UIButton *btn1;//這些控制項不會死,所以可以用weak,@property (weak, nonatomic) IBOutlet UIButton *btn2;@property (weak, nonatomic) IBOutlet UIButton *btn3;@end/** *  盡量少使用tag:    1> tag的效率非常差    2> tag使用多了,容易亂 */@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        // 根據tag拿到對應的view    UIView *redView = [self.view viewWithTag:1];    self.redView = redView;           // 1.1 建立UISwitch對象    UISwitch *sw = [[UISwitch alloc] init];       // 1.2 加到控制器的view中    [self.view addSubview:sw];        // 1.3 建立UISwitch對象    UISwitch *sw1 = [[UISwitch alloc] init];        // 1.4 加到紅色的view    [redView addSubview:sw1];        // 1.5 建立一個選項卡對象    UISegmentedControl *sg = [[UISegmentedControl alloc] initWithItems:@[@"哈哈哈", @"??", @"嘻嘻"]];    // 1.6 加到紅色的view    [redView addSubview:sg];        // 1.7 移除//  [sg removeFromSuperview];//  [self.redView removeFromSuperview];//  [sw removeFromSuperview];    [self.view removeFromSuperview];// 這裡移除不了    }#pragma mark - 虛擬碼---viewWithTag,所以通過tag效率很差/* - (UIView *)viewWithTag: (NSInteger)tag{    if (self.tag == tag) return self;    for (UIView *subView in self.subviews) {        if (subView.tag == tag)  return subView;        // 繼續遞迴遍曆        // ..    }}*/// 只要控制項有父控制項,就一定能夠移除- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];//    [self.view removeFromSuperview];  這裡可以移除控制器的view,}- (IBAction)remove {    [self.redView removeFromSuperview];}- (IBAction)clickBtn:(UIButton *)button { //連線就是把方法和控制項串連起來了。    // 做私人的事情        if (button == self.btn1) {        NSLog(@"點擊了按鈕1");    }else if (button == self.btn2) {        NSLog(@"點擊了按鈕2");    }else{        NSLog(@"點擊了按鈕3");    }            switch (button.tag) {        case 3:            NSLog(@"點擊了按鈕1");            break;        case 4:            NSLog(@"點擊了按鈕2");            break;        case 5:            NSLog(@"點擊了按鈕3");            break;        default:            break;    }        // 做公用的事情    NSLog(@"做公用的事情");}@end

 

ios3--UIView的常見方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.