1

What I tried is:

- (void)main
{
    NSError *err = (__bridge NSError *)error;
                    UIAlertController * alert = [UIAlertController
                                                 alertControllerWithTitle:NSLocalizedString(@"Error", nil)
                                                 message:err.localizedDescription
                                                 preferredStyle:UIAlertControllerStyleAlert];


                    UIAlertAction* ok = [UIAlertAction
                                         actionWithTitle:@"OK"
                                         style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction * action)
                                         {
                                             //Do some thing here
                                            [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
                                         }];
...
}

The problem is that there is no access to self.window in my NSOperation subclass. Is there any other way to present alert controller?

4

1 回答 1

1

UIApplicationDelegateinstance 保留 window 对象,因此您可以获取它并用于您的目的。

  [[UIApplication sharedApplication].delegate window]

[UIApplication sharedApplication]提供了其他可能对您有用的方法:

  • - windows- 所有窗口的数组;
  • – keyWindow- 给出正在接收键盘输入的窗口(或 nil);

如果您没有创建额外的窗口,那么使用[[UIApplication sharedApplication].delegate window]就可以了。

于 2014-10-22T08:20:48.267 回答