来自NSOperation的UIAlertController

时间:2014-10-22 08:14:49

标签: objective-c ios8 nsoperation uialertcontroller

我尝试的是:

- (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];
                                         }];
...
}

问题是我的self.window子类中无法访问NSOperation。 还有其他方法来呈现警报控制器吗?

1 个答案:

答案 0 :(得分:1)

UIApplicationDelegate实例保留了窗口对象,因此您可以将其用于您的目的。

  [[UIApplication sharedApplication].delegate window]

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

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

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