carya.liu
carya.liu

Reputation: 51

how can I present a UIAlertController above UIViewController.view.window

Now, I created a UIViewController called viewController, and add a subview called maskView on viewController via:

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

and then on maskView there is a button called "deleteBtn", when click on the deleteBtn, I want to present a UIAlertController via:

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];

but the alertController is not present on the topmost screen, it is below the maskView, how can I show the alertController above the maskView added on UIViewController.view.window?

Thanks so much!

Upvotes: 1

Views: 771

Answers (1)

carya.liu
carya.liu

Reputation: 51

finally I fixed this problem by creating a new window object and switch between this window and the default one, the code like this:

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

and self.alertWindow is defined as:

@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;
}

Upvotes: 1

Related Questions