iOS / Objective-C:从sharedInstance调用AlertViewController

时间:2018-08-22 15:39:54

标签: ios objective-c uialertviewcontroller

我正在更新一些UIAlertViews,自iOS 9.0起不推荐使用UIAlertViewControllers。

使用UIAlertView,可以通过以下简单行从正在执行的任何代码(甚至在实用程序类或共享实例中)发出警报:

[alertView show];

所以,如果我调用一个共享实例,例如

- (void)didTapDeleteButton:(id)sender {
    NSArray *itemsToDelete = [self.selectedIndexPathToContact allValues];

    [[IDModel sharedInstance] deleteItems:itemsToDelete];

//包含代码:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Keep Archive Copy?"
                                                    message:nil
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"OK",nil];
alertInvite.alertViewStyle = UIAlertViewStyleDefault;
alertInvite.tag=100;
[alertView show];

一切正常。

但是,对于UIAlertController,这是不允许的。如果将以下代码放在可通过共享实例访问的类的方法中,则当您进入presentViewController时,它将引发错误:

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Delete Item?" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    [alertView dismissViewControllerAnimated:YES completion:nil];
}];

UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"Not Now" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    [alertView dismissViewControllerAnimated:YES completion:nil];
}];

[alertView addAction:noButton];
[alertView addAction:yesButton];
if ([alertView respondsToSelector:@selector(setPreferredAction:)]) {
    [alertView setPreferredAction:yesButton];
}
//FOLLOWING THROWS ERROR
[self presentViewController:alertView animated:YES completion:nil];

在最后一行上,该类(通过共享实例到达)没有此方法。看来您必须使用更复杂的方式来发出警报。我看过一些somwehat convoluted approaches,例如:

id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
    rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
    rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
}
[rootViewController presentViewController:alertInvite animated:YES completion:nil];

但是,这对我不起作用,因为我认为我的共享实例没有rootviewcontroller。有人可以建议一种简单,直接的方法吗?

3 个答案:

答案 0 :(得分:1)

显示我可能想到的任何代码的警报:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction: [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:nil]];
alert.TitleColor = [UIColor whiteColor];

id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
UIViewController *vc = delegate.window.rootViewController;
[vc presentViewController:alert animated:YES completion:nil];

注意:

请注意,在大多数情况下,我不会这样做。
非ui代码不应该执行ui!苹果做出更改的部分原因可能也是这一原因:它鼓励适当的模型||视图分离

答案 1 :(得分:1)

我在UIViewController上创建了一个扩展,该扩展允许我创建一个新窗口并从中显示视图控制器。这使我可以从任何类呈现,而不仅仅是视图控制器。此外,它还可以防止您尝试从已经提供了视图控制器的视图控制器显示警报视图时出现问题。

extension UIViewController {
    func presentFromNewWindow(animated: Bool = true, completion: (() -> Void)? = nil) {
        let window = newWindow()

        if let rootViewController = window.rootViewController {
            window.makeKeyAndVisible()
            rootViewController.present(self, animated: animated, completion: completion)
        }
    }

    private func newWindow() -> UIWindow {
        let window = UIWindow(frame: UIScreen.main.bounds)
        let rootViewController = UIViewController()
        rootViewController.view.backgroundColor = .clear
        window.backgroundColor = .clear
        window.rootViewController = rootViewController
        window.windowLevel = UIWindowLevelAlert

        return window
    }
}

然后您可以使用此方法显示警报控制器(或任何UIViewController):

alertViewController.presentFromNewWindow()

关闭警报视图控制器时,会将其从视图控制器中删除,并将窗口从层次结构中删除。

答案 2 :(得分:0)

我相信您的问题很模糊。但我认为您一直在寻找呈现UIAlertController的示例。那是对的吗?如果是这样,请继续阅读...

示例:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction: [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:nil]];
alert.TitleColor = [UIColor whiteColor];
[self presentViewController:alert animated:YES completion:nil];

doc:https://developer.apple.com/documentation/uikit/uialertcontroller