如何在应用程序委托 (obj-c) 中添加 UIAlertController [英] How do I add a UIAlertController in app delegate (obj-c)

查看:20
本文介绍了如何在应用程序委托 (obj-c) 中添加 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 UIAlertView 被弃用之前使用的代码:

This is the code I was using prior to UIAlertView being deprecated:

//OLD UIALERTVIEW
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    // check to see if newer version exists to alert the user
    BOOL updateAvailable = NO;
    NSDictionary *updateDictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"PLIST_URL_HERE"]];
    NSArray *items = [updateDictionary objectForKey:@"items"];
    NSDictionary *itemDict = [items lastObject];
    NSDictionary *metaData = [itemDict objectForKey:@"metadata"];
    NSString *newversion = [metaData  valueForKey:@"bundle-version"];
    NSString *currentversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

    //updateAvailable = [newversion compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //default code
    updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force update
    //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force no update

    if (updateAvailable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UPDATE AVAILABLE" message:@"Click OK to update the application to the latest version" delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"OK", nil];
        [alert show];
    }
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex]) {
        // point to the pList on dropbox to install the
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"PLIST_URL_HERE"]];
        exit(0);

我正在尝试更新它以改用 UIAlertController.这是我为此使用的代码

I am trying to update it to use UIAlertController instead. heres the code I'm using for that

//NEW UIACTIONCONTROLLER
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    // check to see if newer version exists to alert the user
    BOOL updateAvailable = NO;
    NSDictionary *updateDictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"PLIST_URL_HERE"]];
    NSArray *items = [updateDictionary objectForKey:@"items"];
    NSDictionary *itemDict = [items lastObject];
    NSDictionary *metaData = [itemDict objectForKey:@"metadata"];
    NSString *newversion = [metaData  valueForKey:@"bundle-version"];
    NSString *currentversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

    //updateAvailable = [newversion compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //default code
    updateAvailable = [@"1.3" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force update
    //updateAvailable = [@"1.1" compare:currentversion options:NSNumericSearch] == NSOrderedDescending; //force no update

    if (updateAvailable) {
        UIAlertController * alert=   [UIAlertController alertControllerWithTitle:@"UPDATE AVAILABLE" message:@"Click OK to update the application to the latest version" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"PLIST_URL_HERE"]];
        exit(0);
        }];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:ok];
        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];
    }
}

问题是这段代码:

[self presentViewController:alert animation:YES completion:nil];

[self presentViewController:alert animated:YES completion:nil];

给我这个错误:

/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15:AppDelegate"没有可见的@interface 声明了选择器presentViewController:animated:completion:"

/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15: No visible @interface for 'AppDelegate' declares the selector 'presentViewController:animated:completion:'

可以将该代码放在 viewDidLoad 中,但我不希望每次用户转到 TableView 时都弹出警报.我只希望它仅在应用程序打开时加载.

I can put that code in viewDidLoad but I don't want the alert popping up every time the users go to the TableView. I just want it to load ONLY when the app is opened.

我该怎么做?

推荐答案

Per Apple 文档UIAlertController 就像任何其他需要视图控制器作为基础来呈现的视图控制器一样.

Per Apple Documentation, UIAlertController is just like any another view controller which needs a view controller as base to present on.

要从 AppDelegate 呈现一个 UIAlertController,您可以创建一个临时窗口,其中包含一个空的视图控制器并在其顶部呈现.像这样:

To present an UIAlertController from AppDelegate, you can create a temporary window with an empty view controller in it and present on top of it. Something like this:

self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:nil];

警报解除后,此窗口将自动从内存中取出.

如果 rootViewController 已初始化并添加到视图层次结构中,您还可以在它上显示警报.

You can also present your alert on rootViewController if it has been initialized and is added in view hierarchy.

我希望你明白为什么你现在会收到这个错误.从技术上讲,AppDelegate 不是视图控制器,因此不会响应 presentViewController:animated:completion: 方法.

I hope you got why you are getting this error now. Technically, AppDelegate is not a view controller and so does not respond to presentViewController:animated:completion: method.

这篇关于如何在应用程序委托 (obj-c) 中添加 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆