Objective.C中self.view.widow和[[UIApplication sharedApplication] keyWindow]之间的区别是什么

时间:2015-11-22 15:10:58

标签: objective-c uialertview

如果我喜欢这个

var primeStringGenerator = ["60","52","81","61","85"].generate()
@IBOutlet var PrimeLabel: UILabel!

@IBAction func NewAction(sender: AnyObject) {
    PrimeLabel.text = primeGenerator.next() ?? "n/a"
}

然后在- (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]; } 调用返回

后,红色视图和警报将自动关闭

enter image description here

谁可以告诉我原因?

Objective.C中self.view.widow和[[UIApplication sharedApplication] keyWindow]之间的区别是什么

1 个答案:

答案 0 :(得分:0)

您的问题基本上在这里解决:

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

要将它与问题联系起来,UIAlertView将显示在系统窗口中,而这个窗口是显示警报时的keyWindow。当警报被取消时,该窗口消失,因此您的视图消失的原因。

假设self.view是应用程序窗口的子视图(几乎总是如此),您的视图将继续显示,因为在关闭警报后窗口本身会继续显示。