码迷,mamicode.com
首页 > 其他好文 > 详细

021插入或删除视图中的子元素

时间:2015-06-15 00:03:43      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController {
4     @private
5     UILabel *_lblParent;
6 }
7 
8 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)subviewsDidPush;
  5 @end
  6 
  7 @implementation ViewController
  8 - (void)viewDidLoad {
  9     [super viewDidLoad];
 10     //追加父标签_lblParent
 11     _lblParent = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 320)];
 12     _lblParent.textAlignment = NSTextAlignmentCenter;
 13     _lblParent.text = @"Parent";
 14     _lblParent.layer.borderColor = [UIColor blackColor].CGColor;
 15     _lblParent.layer.borderWidth = 2.0;
 16     [self.view addSubview:_lblParent];
 17     
 18     //追加1个子标签lblChild3
 19     UILabel *lblChild3 = [[UILabel alloc] initWithFrame:CGRectZero];
 20     lblChild3.text = @"Child 3";
 21     [lblChild3 sizeToFit];
 22     lblChild3.backgroundColor = [UIColor grayColor];
 23     lblChild3.layer.borderColor = [UIColor blackColor].CGColor;
 24     lblChild3.layer.borderWidth = 2.0;
 25     [_lblParent addSubview:lblChild3];
 26     
 27     //在上一个标签lblChild3(下面)插入新的标签lblChild1
 28     UILabel *lblChild1 = [[UILabel alloc] initWithFrame:CGRectZero];
 29     CGPoint newPoint = lblChild1.center;
 30     newPoint.x += 10;
 31     lblChild1.center = newPoint;
 32     lblChild1.text = @"Child 1";
 33     [lblChild1 sizeToFit];
 34     lblChild1.backgroundColor = [UIColor orangeColor];
 35     lblChild1.layer.borderColor = [UIColor blackColor].CGColor;
 36     lblChild1.layer.borderWidth = 2.0;
 37     [_lblParent insertSubview:lblChild1 belowSubview:lblChild3];
 38     
 39     //在标签lblChild1追加标签lblChild2
 40     UILabel *lblChild2 = [[UILabel alloc] initWithFrame:CGRectZero];
 41     newPoint.x += 10;
 42     lblChild2.center = newPoint;
 43     lblChild2.text = @"Child 2";
 44     [lblChild2 sizeToFit];
 45     lblChild2.backgroundColor = [UIColor greenColor];
 46     lblChild1.layer.borderColor = [UIColor blackColor].CGColor;
 47     lblChild1.layer.borderWidth = 2.0;
 48     [_lblParent insertSubview:lblChild2 aboveSubview:lblChild1];
 49     
 50     //让标签lblChild3和标签lblChild1交换
 51     [_lblParent exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
 52     
 53     //如果标签lblChild3是标签_lblParent的子元素的话,就删除标签lblChild3
 54     if ([lblChild3 isDescendantOfView:_lblParent]) {
 55         [lblChild3 removeFromSuperview];
 56     }
 57     
 58     //追加弹出提示的按钮
 59     UIButton *btnMessage = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 60     btnMessage.frame = CGRectMake(0, 0, 250, 40);
 61     newPoint = self.view.center;
 62     newPoint.y = self.view.frame.size.height - 40;
 63     btnMessage.center = newPoint;
 64     [btnMessage setTitle:@"显示_lblParent的subviews信息" forState:UIControlStateNormal];
 65     [btnMessage addTarget:self action:@selector(subviewsDidPush) forControlEvents:UIControlEventTouchUpInside];
 66     btnMessage.layer.borderColor = [UIColor blackColor].CGColor;
 67     btnMessage.layer.masksToBounds = YES;
 68     btnMessage.layer.cornerRadius = 8.0;
 69     btnMessage.layer.borderWidth =2.0;
 70     [self.view addSubview:btnMessage];
 71 }
 72 
 73 - (void)didReceiveMemoryWarning {
 74     [super didReceiveMemoryWarning];
 75     // Dispose of any resources that can be recreated.
 76 }
 77 
 78 #pragma mark - Private Methods
 79 - (void)subviewsDidPush {
 80     NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:8];
 81     [mString setString:@""];
 82     for (id view in _lblParent.subviews) {
 83         NSString *addString;
 84         if ([view isKindOfClass:[UILabel class]]) {
 85             addString = ((UILabel *)view).text;
 86         } else {
 87             addString = [view description];
 88         }
 89         if (mString.length > 0) {
 90             [mString appendString:@", "];
 91         }
 92         [mString appendString:addString];
 93     }
 94     UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"The subviews of _lblParent"
 95                                                      message:mString
 96                                                     delegate:nil
 97                                            cancelButtonTitle:nil
 98                                            otherButtonTitles:@"OK", nil];
 99     [alertV show];
100 }
101 
102 @end

 

021插入或删除视图中的子元素

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4575978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!