ios開發之UI中父子視圖關係



#import "AppDelegate.h"app


@interface AppDelegate ()ide


@end ui


@implementation AppDelegatethis



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

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

    self.window.backgroundColor = [UIColor whiteColor];.net

    self.window.rootViewController = [[UIViewController alloc] init];3d

    

    //=======================================rest

    //1.一個視圖只能有一個父視圖;一個視圖能夠有多個子視圖orm

    //建立一個視圖對象

    UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

    redView.backgroundColor = [UIColor redColor];

    //設置tag

    redView.tag = 100;

    //將紅色視圖添加到window

    [self.window addSubview:redView];

    

    

    //建立一個綠色視圖對象

    UIView * greenView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];

    greenView.backgroundColor = [UIColor greenColor];

    

    //設置tag

    greenView.tag = 200;

    

    //將綠色視圖添加到window

    [self.window addSubview:greenView];

    

    //一個視圖被屢次添加到其餘視圖上,最後一次添加有效

    [redView addSubview:greenView];

    

    //2.獲取一個視圖的父視圖

    //@property(nonatomic,readonly) UIView       *superview;

    UIView * greenSuper = [greenView superview];

    greenSuper.backgroundColor = [UIColor yellowColor];

    

    //3.獲取視圖上全部的子視圖

    //注意:有的時候可能會獲取到一些咱們意料之外的視圖,

    //@property(nonatomic,readonly,copy) NSArray *subviews;

    NSArray * array = [self.window subviews];

    NSLog(@"%@", array);

    

    //4.獲取window(應用程序中,程序啓動後界面上全部的視圖都是直接或者間接的添加到window,因此能夠經過界面上全部的視圖拿到當前應用的window

    //前提:獲取window的時候,已經顯示出來

    //@property(nonatomic,readonly) UIWindow     *window;

    UIWindow * twindow = [greenView window];

    twindow.backgroundColor = [UIColor lightGrayColor];

    

    //5.將視圖從父視圖上移除

    //添加按鈕

    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(300, 400, 60, 40)];

    btn.backgroundColor = [UIColor purpleColor];

    [btn addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:btn];

    

    //移除(將視圖從父視圖上移除)

    //一旦視圖從父視圖上移除,那麼父視圖就不能再經過viewWithTag:subViews來獲取到當前視圖

    [greenView removeFromSuperview];

    

    //已經從父視圖上移除的視圖,在內存中仍是存在的;移除只是不讓這個視圖顯示在父視圖上而已

    [redView addSubview:greenView];

    

    

    

    

    

    

 

    

    [self.window makeKeyAndVisible];

    return YES;

}


#pragma mark - 按鈕點擊

- (void)onclicked:(UIButton *)btn{


//    static BOOL tbool = YES;

//    

//    if (tbool) {

//        

//        

//    }

}


- (void)applicationWillResignActive:(UIApplication *)application {

    


    NSArray * array = [_window subviews];

    NSLog(@"%@", array);

    

    

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end

相關文章
相關標籤/搜索