如何在app delegate(obj-c)中添加UIAlertController

时间:2015-10-15 02:41:07

标签: ios objective-c uialertcontroller

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

Xamarin.Android.GooglePlayservice.Plus

我正在尝试更新它以使用UIAlertController。 继承了我用于

的代码
//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);

问题是这段代码:

  

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

给了我这个错误:

  

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

可以将该代码放在//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]; } } 中,但我不希望每次用户转到TableView时都会弹出警报。我只想在应用程序打开时加载它。

我该怎么做?

2 个答案:

答案 0 :(得分:10)

Apple DocumentationUIAlertController就像任何另一个需要视图控制器作为基础的视图控制器一样。

要从UIAlertController显示AppDelegate,您可以创建一个临时窗口,其中包含一个空视图控制器并显示在其上。像这样:

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上显示提醒。

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

答案 1 :(得分:1)

  let navigationController = application.windows[0].rootViewController as UINavigationController

            let activeViewCont = navigationController.visibleViewController

            activeViewCont.presentViewController(alertController, animated: true, completion: nil)