ios开发之UI中父子视图关系



#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



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

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

    self.window.backgroundColor = [UIColor whiteColor];

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

    

    //=======================================

    //1.一个视图只能有一个父视图;一个视图可以有多个子视图

    //创建一个视图对象

    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


转载于:https://my.oschina.net/luhoney/blog/655423

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值