视图的层次结构

一个视图是一个容器,当一个视图包含其他视图的时候,两个视图之间就建立一个父与子的关系,被包含的是是子视图(subView)。包含的是父视图(superView)

每一个视图是以一个有序的数组存储着他的子视图,存储的顺序会影响他的显示效果

一个视图只有一个superView,但是可以有多个subView

从视觉上,子视图会覆盖父视图

当程序退出的时候,weindow才会释放掉

视图的生命周期是和程序绑定的

#import <UIKit/UIKit.h>


@interface liAppDelegate : UIResponder <UIApplicationDelegate>

{

    UIView *view1;

}


@property (strong, nonatomic) UIWindow *window;


@end

#import "liAppDelegate.h"


@implementation liAppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    view1  = [[UIView alloc]initWithFrame:CGRectMake(60, 100, 200, 100)];

    view1.backgroundColor = [UIColor yellowColor];

    [self.window addSubview:view1];

    [view1 release];

    //后边加的视图会遮挡住前边加的视图

    //UIView *view2  = [[UIView alloc]initWithFrame:CGRectMake(60, 150, 200, 100)];

    UIView *view2  = [[UIView alloc]initWithFrame:CGRectMake(60, 150, 200, 100)];

    view2.backgroundColor = [UIColor redColor];

    [self.window addSubview:view2];

    //[view1 addSubview:view2];

    [view2 release];

    

    UIView *view3  = [[UIView alloc]initWithFrame:CGRectMake(60, 250, 200, 100)];

    view3.backgroundColor = [UIColor blueColor];

    [self.window addSubview:view3];

    [view3 release];

    

    /*NSLog(@"view1 %@",[view1 superview]);

    NSLog(@"view2 %@",[view2 superview]);

    

    NSLog(@"self.window subview:%@",self.window.subviews);

    NSLog(@"view1.subView:%@",view1.subviews);

    */

    

    //创建一个button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake( 90, 400,140, 35);

    button.backgroundColor = [UIColor purpleColor];

    [button addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button];

    

    NSLog(@"subViews array : %@",self.window.subviews);


    return YES;

}

- (void) changeView:(id)sender

{

    [self.window bringSubviewToFront:view1];

    NSLog(@"subViews array : %@",self.window.subviews);

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值