window,多个时,[UIApplication sharedApplication].windows[0]和[[UIApplication sharedApplication] delegate] window]获取的是最开始创建的window,而[UIApplication sharedApplication].keyWindow获取的是当前显示的window,不一定是最初的window。window之间的切换时可以的,让不需要出现的window隐藏,不过只有当前显示的window才能响应事件,window隐藏,它上面所添加的VIew都会隐藏。就是window不隐藏,它后面有window添加,那么相当于开始的是隐藏的,它上面的东西不会显示出来。
直接怼代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
wgj01ViewController *mainTabBarController = [[wgj01ViewController alloc] init];
UINavigationController *naBar = [[UINavigationController alloc] initWithRootViewController:mainTabBarController];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = naBar;
[self.window makeKeyAndVisible];
NSLog(@"root1N:%@,,root1VC:%@",naBar,mainTabBarController);
self.secondWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
wgj02ViewController *secondVC = [[wgj02ViewController alloc] init];
self.secondWindow.rootViewController = secondVC;
[self.secondWindow makeKeyAndVisible];
self.secondWindow.backgroundColor = [UIColor orangeColor];
// [self.window addSubview:self.secondWindow];
NSLog(@"root2:%@",secondVC);
NSLog(@"keyW:%@", [UIApplication sharedApplication].keyWindow);
NSLog(@"windows:count:%ld,具体:%@\n%@",[UIApplication sharedApplication].windows.count ,[UIApplication sharedApplication].windows[0],[UIApplication sharedApplication].windows[1]);
NSLog(@"\n\n\ndelegateW:%@\n%@", [[[UIApplication sharedApplication] delegate] window],[UIApplication sharedApplication].windows[0].rootViewController);
return YES;
}
#import "wgj01ViewController.h"
#import "wgj01push1.h"
@implementation wgj01ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 200, 50)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"wgj01ViewControllerAddView" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 250, 200, 50)];
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"wgj01ViewControllerPush" forState:(UIControlStateNormal)];
[button1 addTarget:self action:@selector(clickBtnPush) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
}
- (void)clickBtn
{
NSLog(@"点击了wgj01ViewController");
NSLog(@"\n\nself.view.window:%@\n,keyWindow:%@\n,delegateWindow:%@\n,count:%lu\n\n%@\n%@\n",self.view.window,[UIApplication sharedApplication].keyWindow,[[[UIApplication sharedApplication] delegate] window],(unsigned long)[UIApplication sharedApplication].windows.count,[UIApplication sharedApplication].windows[0],[UIApplication sharedApplication].windows[1]);
UIView *coverV = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
coverV.backgroundColor = [UIColor redColor];
[[UIApplication sharedApplication].windows[0] addSubview:coverV];
}
- (void)clickBtnPush
{
wgj01push1 *ss = [[wgj01push1 alloc] init];
[self.navigationController pushViewController:ss animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
\#import "wgj02ViewController.h"
@implementation wgj02ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, 200, 50)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"wgj02ViewController" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void)clickBtn
{
// UIView *coverV = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
// coverV.backgroundColor = [UIColor redColor];
// [[UIApplication sharedApplication].windows[0] addSubview:coverV];
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"点击了wgj02ViewController");
NSLog(@"\n\n%@",self.view.window);
[self.view.window resignKeyWindow];
NSLog(@"%@,%@",[[[UIApplication sharedApplication] delegate] window],[UIApplication sharedApplication].keyWindow );
self.view.window.hidden = YES;
NSLog(@"%@,%@",[[[UIApplication sharedApplication] delegate] window],[UIApplication sharedApplication].keyWindow );
// });
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
// view.backgroundColor = [UIColor orangeColor];
// [[[UIApplication sharedApplication].delegate window] addSubview:view];
//
// UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
// [window addSubview:view];
// [window makeKeyAndVisible];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "wgj01push1.h"
@implementation wgj01push1
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 350, 200, 50)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"wgj01push1Title" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
@end
网友评论
怎么会有3个window?