什么';这是Objective-C中self.view.widow和[[UIApplication sharedApplication]keyWindow]之间的区别
如果我真的喜欢这个什么';这是Objective-C中self.view.widow和[[UIApplication sharedApplication]keyWindow]之间的区别,objective-c,uialertview,Objective C,Uialertview,如果我真的喜欢这个 - (void)viewDidLoad { [super viewDidLoad]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"test" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; } // Called when a button is cli
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"test" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
// Called when a button is clicked. The red view and alert will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self showView];
}
// after animation ,this will work fine
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// [self showView];
}
- (void)showView {
// Called when a button is clicked. The view will be automatically dismissed after this call returns
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, 200, 200);
view.backgroundColor = [UIColor redColor];
view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.0f, [UIScreen mainScreen].bounds.size.height/2.0f);
// [self.view.window addSubview:view]; //when i use this ,both of the two delegate methods works fine
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubview:view];
}
然后,红色视图和警报将在alertView:ClickedButtonIndex:
调用返回后自动解除
谁能告诉我原因
Objective-C中的self.view.widow和[[UIApplication sharedApplication]keyWindow]有什么区别 为了将其与您的问题联系起来,UIAlertView将显示在一个系统窗口中,并且在显示您的警报时,该窗口就是关键窗口。当警报解除时,该窗口将消失,因此您的视图将消失
假设self.view是应用程序窗口的子视图(几乎总是如此),您的视图将继续显示,因为在警报解除后窗口本身将继续显示。最后一段不太正确
self.view.window
是视图添加到的任何窗口。它可能是主应用程序窗口。它可能是应用程序创建和使用的辅助窗口。它可能是一个系统窗口。它很可能是应用程序的主窗口,但不一定是。OP应该澄清一下,但我假设在这个特定示例中使用self.view意味着该视图是视图控制器的视图,它会自动添加到应用程序的窗口中。我已经澄清了我的答案。