2015-11-22 20 views
0

,如果我不喜欢这个什么self.view.widow之间的差异,[[UIApplication的sharedApplication] keyWindow]在Objective-C

- (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:clickedButtonAtIndex:调用返回自动解除

enter image description here

谁可以告诉我原因吗?

什么和[UIApplication的sharedApplication] keyWindow]在Objective-C

回答

0

你的问题基本上是在此解决self.view.widow之间的区别:

diffrence between [[[[UIApplication sharedApplication] delegate] window] and [[UIApplication sharedApplication].keyWindow?

为了配合回你的问题,UIAlertView会出现在系统窗口中,而这个窗口就是显示警报时的keyWindow。当警报解除时,该窗口消失,因此视图消失的原因。

假设self.view是应用程序窗口的子视图(几乎总是这种情况),您的视图将继续显示,因为窗口本身在警报解除后继续显示。

+0

最后一段不太正确。 'self.view.window'是视图添加到的任何窗口。它可能是主要的应用程序窗口。它可能是应用程序创建和使用的辅助窗口。它可能是一个系统窗口。很可能它是应用程序的主窗口,但它不一定是。 – rmaddy

+0

OP应该澄清,但我认为在这个特定的例子中使用self.view意味着该视图是一个视图控制器的视图,它被自动添加到应用程序的窗口。我澄清了我的答案。 – Jose

相关问题