【问题标题】:Presenting a UIAlertController from a modally presented controller that is being dismissed从正在关闭的模态呈现的控制器呈现 UIAlertController
【发布时间】:2015-03-06 03:04:57
【问题描述】:

在 iOS 8 之前,可以在解除 UIViewController 的同时从模态呈现的 UIViewController 中显示 UIAlertView。我发现当用户在按下模态呈现的控制器上的“保存”按钮时需要提醒他们发生了一些变化时,这特别有用。从 iOS 8 开始,如果 UIAlertController 在被解除时从模态呈现的视图控制器中显示,则 UIAlertController 也会被解除。 UIAlertController 在用户阅读或自行关闭之前被关闭。我知道一旦控制器被解除,我可以让模态呈现的控制器的委托显示警报视图,但是这种情况会产生大量额外的工作,因为该控制器在许多地方使用,并且 UIAlertController 必须在某些条件下呈现,在每种情况下都需要将参数传递回控制器委托。有什么方法可以在关闭控制器的同时从模态呈现的控制器(或至少从控制器内的代码)显示 UIAlertController,并让 UIAlertController 一直保持直到它被关闭?

【问题讨论】:

  • 通过创建新窗口从 appDelegate 呈现它是否明智?我听说这可能会导致轮换问题..

标签: objective-c uiviewcontroller ios8 modal-dialog uialertcontroller


【解决方案1】:

您可以在模态控制器类的 dismissViewControllerAnimated 方法的完成块中处理此问题。在 rootviewcontroller 上显示 UIAlertController,它应该在任何类中处理。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationItem.rightBarButtonItem setAction:@selector(dismissView)];
[self.navigationItem.rightBarButtonItem setTarget:self];
}
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:^{
    [self showAlert];
}];
}

- (void)showAlert {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is Alert" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    [alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:okButton];
[alertController addAction:cancelButton];
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多