如何在UIViewController.view.window

时间:2015-04-29 10:39:54

标签: ios8 uialertcontroller

现在,我创建了一个名为viewController的UIViewController,并通过以下方式在viewController上添加一个名为maskView的子视图:

UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
[self.view.window addSubview:maskView];

然后在maskView上有一个名为" deleteBtn"的按钮,当点击deleteBtn时,我想通过以下方式呈现一个UIAlertController:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"mybulb.confirmRemove", @"") message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", @"取消") style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"confirm", @"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[alertController addAction:confirmAction];
[self presentViewController:alertController animated:YES completion:nil];

但是alertController不在最顶层的屏幕上,它在maskView下面,如何在UIViewController.view.window上添加的maskView上面显示alertController?

非常感谢!

1 个答案:

答案 0 :(得分:1)

最后我通过创建一个新的窗口对象来解决这个问题,并在这个窗口和默认窗口之间切换,代码如下:

if ([UIAlertController class]) {

  AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"readus.org.title", @"alert title") message:@"" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"readus.org.cancel", @"取消") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  [appDelegate.window makeKeyAndVisible];
  }];
  UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"readus.org.confirm", @"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  //do anything you like...
  [appDelegate.window makeKeyAndVisible];
  }];
  [alertController addAction:cancelAction];
  [alertController addAction:confirmAction];

  [self.alertWindow makeKeyAndVisible];
  [self.alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

和self.alertWindow定义为:

@property (strong, nonatomic) UIWindow *alertWindow;

- (UIWindow *)alertWindow {
    if (!_alertWindow) {
        _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        UIViewController *viewController = [[UIViewController alloc] init];
        _alertWindow.rootViewController = viewController;
    }

    return _alertWindow;
}