The iOS 5 Developer’s Cookbook 6 Assembing Views and Animations

6.1 View Hierarchies    确实代码很妙,// Recursively travel down the view tree, increasing the// indentation level for children- (void) dumpView: (UIView *) aView atIndent: (int) indent into: (NSMu
摘要由CSDN通过智能技术生成

6.1 View Hierarchies    

确实代码很妙,

// Recursively travel down the view tree, increasing the

// indentation level for children

- (void) dumpView: (UIView *) aView atIndent: (int) indent into: (NSMutableString *) outstring

{

    // Add the indentation

    for (int i = 0; i < indent; i++)

        [outstring appendString:@"--"];

    

    // Add the class description

    [outstring appendFormat:@"[%2d] %@\n", indent,

     [[aView class] description]];

    

    // Recurse on subviews

    for (UIView *view in aView.subviews)

        [self dumpView:view atIndent:indent + 1 into:outstring];

}


// Start the tree recursion at level 0 with the root view

- (void) dumpTree: (id) sender

{

    NSMutableString *outstring = [NSMutableString string];

    [self dumpView:self.view atIndent:0 into:outstring];

    NSLog(@"%@", outstring);

}

6.2 主要是介绍了view 的几个方法的使用,可以结合例子理解。递归

The subviewsmethod returns just those views that are immediate children of a givenview 

A call to[[UIApplication sharedApplication] windows]returns the array of application windows. 

Theview’s windowproperty points to the window that owns it. 

TheisDescendantOfView:method determines whether a view lives within another view,even if that view is not its direct superview. 

6.3 Managing Subviews    


Adding Subviews      

ninsertSubview:atIndex:
ninsertSubview:aboveSubview:

ninsertSubview:belowSubview: 

viewWithTag 这个方法很好用,因为这个查找是一个递归的查找,不需要查找结果是直接的子视图。因此直接用window即可,

UILabel *label = (UILabel *)[self.view.window viewWithTag:101];

label.text = @"Hello World"; 



UIView的这个TagExtensions 很好,底下是用法。

  • -  (IBAction)updateTime:(id)sender{

    // set the label to the current time[self.view.window labelWithTag:LABEL_TAG].text =

    [[NSDate date] description];

    }

  • -  (IBAction)updateSwitch:(id)sender{

    // toggle the switch from its current setting
    UISwitch *s = [self.view.window switchWithTag:SWITCH_TAG];[s setOn:!s.isOn];


@interface UIView (TagExtensions)
- (UILabel *) labelWithTag: (NSInteger) aTag;- (UISwitch *) switchWithTag: (NSInteger) aTag;@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值