如何在app delegate(obj-c)中添加UIAlertController
这是我在弃用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]; } }
问题是这段代码:
[self presentViewController:alert animated:YES completion:nil];
给我这个错误:
/Users/liz/Desktop/CactusQueuesGit/CactusQueue/AppDelegate.m:154:15:’AppDelegate’没有可见的@interface声明选择器’presentViewController:animated:completion:’
我可以将该代码放在viewDidLoad
但我不希望每次用户转到TableView时都会弹出警报。 我只想在应用程序打开时加载它。
我该怎么做呢?
根据Apple文档 , UIAlertController
就像任何另一个需要视图控制器作为基础的视图控制器一样。
要从AppDelegate
呈现UIAlertController
,您可以创建一个临时窗口,其中包含一个空视图控制器并显示在其上。 像这样的东西:
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:
方法。
let navigationController = application.windows[0].rootViewController as UINavigationController let activeViewCont = navigationController.visibleViewController activeViewCont.presentViewController(alertController, animated: true, completion: nil)
- 再次访问SceneKit:第1部分
- 获取iOS上所有联系人的列表
- 玩CAGradientLayer — Swift 3.0
- kCVPixelFormatType_420YpCbCr8BiPlanarFullRange帧到UIImage转换
- 核心数据—将NSManagedObject数组转换为json数组
- 在启用素材资源库的情况下修改Xcode 5中的Info.plist的CFBundleVersion
- 查看ApplicationWillResignActive中的控制器variables
- YTPlayerView youtube-ios-player-helper暂停不工作
- 没有find框架-fobjc-arc