【问题标题】:UIAlertView first deprecated IOS 9UIAlertView 首先弃用 IOS 9
【发布时间】:2015-12-17 20:24:06
【问题描述】:

我尝试了几种方法来使用 UIAlertController,而不是 UIAlertView。我尝试了几种方法,但无法使警报操作起作用。 这是我的代码,在 IOS 8 和 IOS 9 中运行良好,但出现了不推荐使用的标志。我尝试了下面的优雅建议,但我无法使其在这种情况下发挥作用。我需要提交我的应用程序,这是最后要解决的问题。感谢您提供任何进一步的建议。我是新手。

#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];

        }

}

【问题讨论】:

  • 问题不清楚。你想要什么?
  • UIAlertView 在 IOS 9 中已被弃用,我们必须使用 UIAlertController 来代替 UIAlertControllerStyleAlert 的首选样式。但是,当我使用 UIAlertController 方法时,警报不会显示。我尝试了下面的解决方案,但仍然没有显示警报视图..谢谢..
  • 好的。你能证明你做了什么,但没有用吗?
  • 展示后你正在调用 [self dismissViewControllerAnimated:true completion:nil];这将关闭警报控制器
  • 这是一个重要的问题,因为 UIAlertController 还很新,许多开发人员会担心它会被弃用。不合理的否决票会对有效问题产生不良影响。那些对问题投反对票的人应该在编辑后改变他们的投票。但是,那些因投反对票而获得徽章的“投反对票巨魔”无论如何都不会这样做。 SO 版主应该有办法解决它。

标签: ios objective-c uialertview uialertcontroller


【解决方案1】:
-(void)showAlert{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                   message:@"Message"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}

[self showAlert]; // 调用方法

【讨论】:

    【解决方案2】:

    从 iOS8 开始,Apple 提供了新的 UIAlertController 类,您可以使用它来代替现在已弃用的 UIAlertView,它也在弃用消息中说明:

    UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用 的 UIAlertControllerStyleAlert 代替

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

    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];
    

    【讨论】:

    • 感谢您的快速回复。我试过了,但警报视图没有显示。我添加“是请”操作时也没有。
    • 你能告诉我你的代码吗,因为它对我来说工作正常,确保你在主线程上执行这个代码
    • 你能在问题中添加你的代码,这样每个人都能看到会更好
    • 您好,我添加了适用于 IOS8 和 IOS9 的完整代码,但已被标记为已弃用。我尝试了几种方法来实现您的代码,但没有显示警报,并且打开商店 ViewController 的操作也不起作用。感谢您的帮助。
    • 我认为最好从操作处理程序中删除这一行[alert dismissViewControllerAnimated:YES completion:nil];,因为没有此代码,警报会自动隐藏。此外,可能会错误地将代码放入将不会执行的完成块中。干杯:)
    【解决方案3】:
     UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Are you sure you want to logout?"
                                     message:@""
                                     preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction* yesButton = [UIAlertAction
                                    actionWithTitle:@"Logout"
                                    style:UIAlertActionStyleDestructive
                                    handler:^(UIAlertAction * action)
                                    {
    
                                    }];
    
        UIAlertAction* noButton = [UIAlertAction
                                   actionWithTitle:@"Cancel"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
                                       //Handle no, thanks button
                                   }];
    
        [alert addAction:noButton];
        [alert addAction:yesButton];
    
        [self presentViewController:alert animated:YES completion:nil];
    

    【讨论】:

    • 这只是@Adnan Aftab 帖子的转发。
    【解决方案4】:

    Xcode 8 + 斯威夫特

    假设selfUIViewController

    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
        }
    }
    

    【讨论】:

    • if self is view 我们如何做到这一点?
    • 视图与业务逻辑无关。重新考虑您的架构,以便控制器呈现视图(MVVM 或 MVC)
    【解决方案5】:

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

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

    请参阅Alternative to UIAlertView for iOS 9?

    【讨论】:

      【解决方案6】:

      将 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];
      
       }];
      

      注意:如果您想添加两个以上的按钮,请添加另一个 更多 UIAlertAction 到 UIAlertController。

      【讨论】:

        【解决方案7】:

        检查一下:

        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 
        
        {
        
        }
        

        【讨论】:

          【解决方案8】:

          使用 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];
          }
          

          【讨论】:

            【解决方案9】:
            //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:^{
                }];
            });
            }
            

            【讨论】:

              【解决方案10】:

              新实现的 Swift 版本是:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2021-01-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-03-04
                • 2018-06-07
                相关资源
                最近更新 更多