一个视图是一个容器,当一个视图包含其他视图的时候,两个视图之间就建立一个父与子的关系,被包含的是是子视图(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);
}