UIAlertView first deprecated IOS 9

后端 未结 10 1231
南笙
南笙 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:46

    Check this:

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

    Xcode 8 + Swift

    Assuming self is a 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
        }
    }
    
    0 讨论(0)
  • 2020-11-28 02:48
     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];
    
    0 讨论(0)
  • 2020-11-28 02:49

    Make UIAlertController+AlertController Category as:

    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
    

    in your 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];
    
     }];
    

    NOTE: If you want to add more than two buttons then add another more UIAlertAction to the UIAlertController.

    0 讨论(0)
  • 2020-11-28 02:50

    Use UIAlertController instead of 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];
    }
    
    0 讨论(0)
  • 2020-11-28 02:51
    -(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]; // calling Method

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