objective-c - 从模态呈现的 Controller 中呈现 UIAlertController,该 Controller 将被关闭

标签 objective-c uiviewcontroller ios8 modal-dialog uialertcontroller

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

最佳答案

您可以在模态 Controller 类的 dismissViewControllerAnimated 方法的完成 block 中处理此问题。在应该在任何类中处理的 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];
}

关于objective-c - 从模态呈现的 Controller 中呈现 UIAlertController,该 Controller 将被关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28891723/

相关文章:

ios - UIView 的特定部分放大/缩小而不增加 View objective C iOS 的高度/宽度

iOS 使用 UIGraphicsGetImageFromCurrentImageContext 检测屏幕上的某些像素

ios - 以编程方式设置 UIViewController 的 View 和生命周期

ios - 如何使用委托(delegate)在其他 Storyboard 中的 vc 的 MainStoryboard 中显示 ViewController?

Swift:Corelocation 处理 didFailWithError 中的 NSError

memory-management - 奇怪的,零星的 CUI ... NSInvalidArgumentException 错误

ios - 'GADUNativeCustomTemplateAd' 错误没有可见的@interface

objective-c - 如何在 nsuserdefaults 或任何其他有效方式中保存 slider 值和标签值?

ios - 在 SWIFT 中的不同 View 之间切换

ios - 在 iOS 8 中更改 UISegmentedControl 的边框宽度