UIAlertView首先不赞成使用IOS 9

我已经尝试了几种使用UIAlertController而不是UIAlertView的方法。 我尝试了几种方法,但是无法使警报操作起作用。 这里是我的代码在IOS 8和IOS 9中正常工作,但显示不赞成使用的标志。 我尝试了下面的优雅build议,但是我不能在这方面使它起作用。 我需要提交我的应用程序,这是最后要解决的问题。 感谢您的任何进一步的build议。 我是一个新手。

#pragma mark - BUTTONS ================================ - (IBAction)showModesAction:(id)sender { NSLog(@"iapMade: %d", iapMade3); // IAP MADE ! =========================================== if (!iapMade3) { //start game here gamePlaysCount++; [[NSUserDefaults standardUserDefaults]setInteger:gamePlaysCount forKey:@"gamePlaysCount"]; NSLog(@"playsCount: %ld", (long)gamePlaysCount); if (gamePlaysCount >= 4) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic" message: THREE_PLAYS_LIMIT_MESSAGE delegate:self cancelButtonTitle:@"Yes, please" otherButtonTitles:@"No, thanks", nil]; [alert show]; NSString *path = [[NSBundle mainBundle] pathForResource:@"cow" ofType:@"wav"]; _pop =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [_pop play]; [self dismissViewControllerAnimated:true completion:nil]; } else { if (gamePlaysCount == 1) { // Create & store the next 5 mins when player gets 3 more lives nextDateToPlay = [[NSDate date] dateByAddingTimeInterval:60*60*0.1]; NSLog(@"CURRENT DATE: %@", [NSDate date]); NSLog(@"NEXT DAY: %@", nextDateToPlay); [[NSUserDefaults standardUserDefaults]setObject: nextDateToPlay forKey:@"nextDateToPlay"]; NSLog(@"nextDateToPlay: %@", nextDateToPlay); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic" message: THREE_PLAYS_LIMIT_MESSAGE2 delegate:self cancelButtonTitle:@"Got it!" otherButtonTitles:@"Start", nil]; [alert show]; } else { if (gamePlaysCount == 3) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic" message: THREE_PLAYS_LIMIT_MESSAGE3 delegate:self cancelButtonTitle:@"Yep, I Know!" otherButtonTitles:@"Start", nil]; [alert show]; } } } } } // IAP NOT MADE ============================= #pragma mark - ALERTVIEW DELEGATE ============================ -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Yes, please"]) { UIStoryboard *storyboard = self.storyboard; MenuViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Store"]; svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:svc animated:YES completion:nil]; } } 

从iOS8苹果提供了新的UIAlertController类,您可以使用,而不是UIAlertView现在已经弃用,它也是在折旧信息

UIAlertView已被弃用。 相反,使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle

所以你应该使用这样的东西

 UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes, please" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Handle your yes please button action here }]; UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No, thanks" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Handle no, thanks button }]; [alert addAction:yesButton]; [alert addAction:noButton]; [self presentViewController:alert animated:YES completion:nil]; 
 //Calling [self showMessage:@"There is no internet connection for this device" withTitle:@"Error"]; //Method -(void)showMessage:(NSString*)message withTitle:(NSString *)title { UIAlertController * alert= [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //do something when click button }]; [alert addAction:okAction]; UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [vc presentViewController:alert animated:YES completion:nil]; } 

如果你想在NSObject类中使用这个警报,你应该使用像:

 -(void)showMessage:(NSString*)message withTitle:(NSString *)title{ dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{ }]; }); } 

Swift版本的新实现是:

  let alert = UIAlertController(title: "Oops!", message:"your message", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "Okay.", style: .Default) { _ in }) self.presentViewController(alert, animated: true){} 

Xcode 8 + Swift

假设self是一个UIViewController

 func displayAlert() { let alert = UIAlertController(title: "Test", message: "I am a modal alert", preferredStyle: .alert) let defaultButton = UIAlertAction(title: "OK", style: .default) {(_) in // your defaultButton action goes here } alert.addAction(defaultButton) present(alert, animated: true) { // completion goes here } } 

使用UIAlertController而不是UIAlertView

 -(void)showMessage:(NSString*)message withTitle:(NSString *)title { UIAlertController * alert= [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //do something when click button }]; [alert addAction:okAction]; UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [vc presentViewController:alert animated:YES completion:nil]; } 

使UIAlertController + AlertController类别为:

UIAlertController + AlertController.h

 typedef void (^UIAlertCompletionBlock) (UIAlertController *alertViewController, NSInteger buttonIndex); @interface UIAlertController (AlertController) + (instancetype)showAlertIn:(UIViewController *)controller WithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitle tapBlock:(UIAlertCompletionBlock)tapBlock; @end 

UIAlertController + AlertController.m

 @implementation UIAlertController (NTAlertController) + (instancetype)showAlertIn:(UIViewController *)controller WithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitle tapBlock:(UIAlertCompletionBlock)tapBlock { UIAlertController *alertController = [self alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; if(cancelButtonTitle != nil) { UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { tapBlock(alertController, ALERTACTION_CANCEL); // CANCEL BUTTON CALL BACK ACTION }]; [alertController addAction:cancelButton]; } if(otherButtonTitle != nil) { UIAlertAction *otherButton = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { tapBlock(alertController, ALERTACTION_OTHER); // OTHER BUTTON CALL BACK ACTION }]; [alertController addAction:otherButton]; } [controller presentViewController:alertController animated:YES completion:nil]; return alertController; } @end 

在您的ViewController.m

 [UIAlertController showAlertIn:self WithTitle:@"" message:@"" cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other" tapBlock:^(UIAlertController *alertController, NSInteger index){ if(index == ALERTACTION_CANCEL){ // CANCEL BUTTON ACTION }else if(index == ALERTACTION_OTHER){ // OTHER BUTTON ACTION } [alertController dismissViewControllerAnimated:YES completion:nil]; }]; 

注意:如果您想要添加两个以上的button,则向UIAlertController添加更多的UIAlertAction。

我试过上面的方法,没有人可以显示alert视图,只有当我把presentViewController:方法放在一个dispatch_async语句中:

dispatch_async(dispatch_get_main_queue(), ^ { [self presentViewController:alert animated:YES completion:nil]; });

请参阅UIAlertView for iOS 9的替代方法? 。

检查这个:

 UIAlertController *alertctrl =[UIAlertController alertControllerWithTitle:@"choose Image" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *camera =[UIAlertAction actionWithTitle:@"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self Action]; //call Action need to perform }]; [alertctrl addAction:camera]; -(void)Action { }