#import "AppDelegate.h"
@interface AppDelegate ()
@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