视图的层次结构(二)

2015-01-27 14:07:53 · 作者: · 浏览: 46
@"test:%@",view2.superview); //6.tag:视图的标签值 //如果想通过tag得到视图,tag设置大于等于100 /* 注意点:1:在同一个父视图上面,两个子视图不能设置相同的tag 2:可以用父视图的父视图找子视图,理解查找机制 */ superView.tag = 100; view2.tag = 101; //7.开启多点触摸 superView.multipleTouchEnabled = YES; //8.开启响应触摸事件 superView.userInteractionEnabled = NO; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.backgroundColor = [UIColor redColor]; button.frame = CGRectMake(90, 90, 90, 90); [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [superView addSubview:button]; UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 290, 40, 40)]; view3.backgroundColor = [UIColor blueColor]; [superView addSubview:view3]; /* clipsToBounds设置为yes后,会将超过父视图的子视图部分截取 */ superView.clipsToBounds = YES; [view3 release]; /*__________________________UIView的内存管理___________________________*/ TestView *testView = [[TestView alloc] initWithFrame:CGRectMake(0, 400, 320, 60)]; testView.backgroundColor = [UIColor cyanColor]; NSLog(@"testView:%d",testView.retainCount); [self.window addSubview:testView]; NSLog(@"testView:%d",testView.retainCount); [testView release]; //1 NSLog(@"testView:%d",testView.retainCount);
//从父视图上移除,被释放
[testView removeFromSuperview]; return YES; } - (void)buttonAction { NSLog(@"buttonAction"); } //程序进入后台 - (void)applicationDidEnterBackground:(UIApplication *)application { //通过tag值取得视图 // UIView *view = [self.window viewWithTag:100]; // view.backgroundColor = [UIColor redColor]; UIView *view = [self.window viewWithTag:101]; view.backgroundColor = [UIColor orangeColor]; } @end