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

//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 代替。
这是我为此使用的代码
//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];
    }
}

问题是这段代码:



给我这个错误:



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

我该怎么做?

最佳答案

对于 Apple DocumentationUIAlertController 就像任何其他需要 View Controller 作为基础来呈现的 View Controller 一样。

要从 UIAlertController 呈现 AppDelegate ,您可以创建一个带有空 View Controller 的临时窗口,并在其顶部呈现。像这样的东西:

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 已初始化并添加到 View 层次结构中,您还可以在 AppDelegate 上显示警报。

我希望你明白为什么你现在会收到这个错误。从技术上讲,presentViewController:animated:completion: 不是 View Controller ,因此不会响应 ojit_code 方法。

关于ios - 如何在应用程序委托(delegate) (obj-c) 中添加 UIAlertController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33138912/

10-13 04:07