,如果我不喜欢这个什么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:
调用返回自动解除
谁可以告诉我原因吗?
什么和[UIApplication的sharedApplication] keyWindow]在Objective-C
最后一段不太正确。 'self.view.window'是视图添加到的任何窗口。它可能是主要的应用程序窗口。它可能是应用程序创建和使用的辅助窗口。它可能是一个系统窗口。很可能它是应用程序的主窗口,但它不一定是。 – rmaddy
OP应该澄清,但我认为在这个特定的例子中使用self.view意味着该视图是一个视图控制器的视图,它被自动添加到应用程序的窗口。我澄清了我的答案。 – Jose