UIAlertViewController错误

时间:2016-02-16 04:22:27

标签: ios iphone

如何在appdelegate类中使用UIAlertViewController。

Getting error no visibility @interface for appdelegate declare the selector

1 个答案:

答案 0 :(得分:1)

如果您希望在UIAlertviewController的{​​{1}}中展示,可以通过两种方式进行展示

<强>选择-1

  

最初你从窗口获得可见/顶级ViewController,假设你当时在任何一个viewcontrollerm的某个地方你从root视图控制器访问它,使用这个

Appdelegate

为了获取可见的视图控件,我可以从here获取答案

    UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    //second add your present on`UIAlertController`
   [vc presentViewController:alert animated:YES Completion:nil];

<强>夫特

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil)
    {
        return rootViewController;
    }
    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];

        return [self visibleViewController:lastViewController];
    }
    if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])
    {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
        UIViewController *selectedViewController = tabBarController.selectedViewController;

        return [self visibleViewController:selectedViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;

    return [self visibleViewController:presentedViewController];
}

<强>选择-2

如果您想直接在 let navigationController = application.windows[0].rootViewController as UINavigationController let vc = navigationController.visibleViewController vc.presentViewController(alert, animated: true, completion: nil) 中展示UIAlertController,请执行简单的

Appdelegetae

<强>更新

 [self.window.rootViewController presentViewController:alert animated:YES Completion:nil];