#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