IOS學習筆記(5)之視圖的層次結構,查找,常用屬性與清理學習(博客地址:http://blog.csdn.net/developer_jiangqq)
Author:hmjiangqq
Email:[email protected]
1.UIView層次結構可以理解為"視圖樹"-View Hierarchy
2.一個視圖就是一個容器,當一個視圖包含其他視圖的時候,兩個視圖之間就建立了
一個父子復習。被包含的視圖被稱為"子視圖(subview)",包含的視圖稱為“父視圖或”
超視圖(superview)"
3.從視覺上看,子視圖隱藏了俯視圖的內容,設置透明屬性可以看到父視圖的內容
4.每個父視圖都有一個有序的數組存儲著它的子視圖,存儲的順序就會影響到每個子視圖
的顯示效果,比如:兩個兄弟視圖重疊在一起,後來被加入的視圖就會出現在另外視圖的上面
5.一個視圖可以嵌入多個subview,但是只能有一個superview
視圖的常用方法
當調用addSubView的時候,會對其進行保留,相當於retain了一個對象,當調用removeFromSuperView時候
會進行釋放該對象,相當於release了該對象
基本的添加和刪除子視圖的方法
- (void)removeFromSuperview; - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; - (void)addSubview:(UIView *)view; - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; - (void)bringSubviewToFront:(UIView *)view; - (void)sendSubviewToBack:(UIView *)view;
- (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]; UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(60, 100, 200, 100)]; view1.backgroundColor=[UIColor redColor]; [self.window addSubview:view1]; [view1 release]; UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; view2.backgroundColor=[UIColor blueColor]; [self.window addSubview:view2]; [view2 release]; NSLog(@"view1 superview:%@",[view1 superview]); NSLog(@"view2 superview:%@",[view2 superview]); NSLog(@"self.window subViews:%d",[[self.window subviews]count]); return YES; }運行截圖:
1.UIView類中又一個tag屬性,通過這個tag屬性可以標示一個視圖對象(整數)
2.獲取的方法,viewWithTag:方法來檢索標示過的子視圖:
[注]:如果tag值不設置,默認的tag值為0;<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Csno1sO6zbLp1dK3vbeotPrC68jnz8I6PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(60, 250, 200, 100)];
view3.tag=100;
view3.backgroundColor=[UIColor yellowColor];
[self.window addSubview:view3];
[view3 release];
NSLog(@"view3:%@",view3);
//通過tag值查找view
UIView *myView=[self.window viewWithTag:100];
myView.alpha=0.1;
NSLog(@"myView:%@",myView);
return YES;
運行截圖:
系統會調用其dealloc方法去釋放對象資源
切記不要手動調用dealloc方法
-(void)dealloc{
//釋放自定義對象
[super dealloc];
}