iOS 8案例开发大全
上QQ阅读APP看书,第一时间看更新

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

实例说明

在iOS应用中,除了使用addSubview方法添加视图外,还可以使用insertSubview方法、insertSubview:aboveSubview方法和insertSubview:belowSubview方法实现。

在本实例中,我们在视图中先追加了父标签parent和子标签child3;然后在上一个标签CHILD 3下插入CHILD 1,在CHILD 1上追加CHILD 2;接下来让CHILD 1与CHILD 3交换;最后删除CHILD 3。

具体实现

实例文件UIkitPrjInsert.h的实现代码如下所示。

#import "SampleBaseController.h"
@interface UIKitPrjInsert : SampleBaseController
{
 @private
  UILabel* parent_;
}
@end

实例文件UIkitPrjInsert.m的实现代码如下所示。

#import "UIKitPrjInsert.h"
#pragma mark ----- Private Methods Definition -----
@interface UIKitPrjInsert ()
- (void)subviewsDidPush;
@end
#pragma mark ----- Start Implementation For Methods -----
@implementation UIKitPrjInsert
// finalize
- (void)dealloc {
  [parent_ release];
  [super dealloc];
}
- (void)viewDidLoad {
  [super viewDidLoad];
  // 将背景设置成黑色
  self.view.backgroundColor = [UIColor blackColor];
  // 追加父标签
  parent_ = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 320, 320 )];
  parent_.textAlignment = UITextAlignmentCenter;
  parent_.text = @"PARENT";
  [self.view addSubview:parent_];
  // 追加1个子标签
  UILabel* child3 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
  child3.text = @"CHILD 3";
  [child3 sizeToFit];
  [parent_ insertSubview:child3 atIndex:0];
  // 在上一个标签CHILD 3 下插入CHILD 1
  UILabel* child1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
  child1.text = @"CHILD 1";
  [child1 sizeToFit];
  [parent_ insertSubview:child1 belowSubview:child3];
  // 在CHILD 1 上追加CHILD 2
  UILabel* child2 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
  child2.text = @"CHILD 2";
  [child2 sizeToFit];
  [parent_ insertSubview:child2 aboveSubview:child1];
  // 让CHILD 1 与CHILD 3 交换
  [parent_ exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
  // 如果CHILD 3 为PARENT子元素的话
  if ( [child3 isDescendantOfView:parent_] ) {
    // 删除CHILD 3
    [child3 removeFromSuperview];
  }
  // 追加subviews按钮
  UIButton* subviewsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  subviewsButton.frame = CGRectMake( 0, 0, 150, 40 );
  CGPoint newPoint = self.view.center;
  newPoint.y = self.view.frame.size.height -40;
  subviewsButton.center = newPoint;
  [subviewsButton setTitle:@"显示subviews" forState:UIControlStateNormal];
  [subviewsButton addTarget:self
                  action:@selector(subviewsDidPush)
          forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:subviewsButton];
}
#pragma mark ----- Private Methods -----
- (void)subviewsDidPush {
  NSMutableString* subviews = [[[NSMutableString alloc] initWithCapacity:64] autorelease];
  [subviews setString:@""];
  // 在subviews的text后附加字符串
  for ( id view in parent_.subviews ) {
    NSString* addString;
    if ( [view isKindOfClass:[UILabel class]] ) {
      addString = ((UILabel*)view).text;
    } else {
      addString = [view description];
    }
    if ( [subviews length] > 0 ) {
      [subviews appendString:@", "];
    }
    [subviews appendString:addString];
  }
  UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"subviews"
                                            message:subviews
                                            delegate:nil
                                    cancelButtonTitle:nil
                                    otherButtonTitles:@"OK", nil ] autorelease];
 [alert show];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}
@end

执行效果如图2-25所示。

图2-25 执行效果