UIAlertView first deprecated IOS 9

后端 未结 10 1216
南笙
南笙 2020-11-28 02:29

I have tried several ways to use UIAlertController,instead of UIAlertView. I tried several ways but I cannot make the alert action work. Here is my code that works fine in I

相关标签:
10条回答
  • 2020-11-28 02:57
    //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];
    }
    

    If you want to use this alert in NSObject class you should use like:

    -(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:^{
        }];
    });
    }
    
    0 讨论(0)
  • 2020-11-28 02:57

    Swift version of new implementation is :

     let alert = UIAlertController(title: "Oops!", message:"your message", preferredStyle: .Alert)
     alert.addAction(UIAlertAction(title: "Okay.", style: .Default) { _ in })
     self.presentViewController(alert, animated: true){}
    
    0 讨论(0)
  • 2020-11-28 03:00

    From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message:

    UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

    So you should use something like this

    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];
    
    0 讨论(0)
  • 2020-11-28 03:09

    I tried the above methods, and no one can show the alert view, only when I put the presentViewController: method in a dispatch_async sentence:

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

    Refer to Alternative to UIAlertView for iOS 9?.

    0 讨论(0)
提交回复
热议问题