,如果我不喜歡這個什麼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