web-dev-qa-db-fra.com

comment ajouter un bouton dans UIAlertController In IOS 9

comment nous utilisons UIAlertView dans iOS 9 et comment ajouter un bouton dans UIAlertController

UIAlertController * alert=[UIAlertController 

alertControllerWithTitle:@"Title" message:@"Message"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction
                        actionWithTitle:@"Yes, please"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {
                            **//What we write here????????**


                        }];
UIAlertAction* noButton = [UIAlertAction
                            actionWithTitle:@"No, thanks"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
                               **//What we write here????????**

                           }];

[alert addAction:yesButton];
[alert addAction:noButton];

[self presentViewController:alert animated:YES completion:nil];
11
Anup Gupta
UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
                                                              message:@"Message"
                                                       preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes, please"
                                                    style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action)
{
    /** What we write here???????? **/
    NSLog(@"you pressed Yes, please button");

    // call method whatever u need
}];

UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No, thanks"
                                                    style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action)
{
    /** What we write here???????? **/
    NSLog(@"you pressed No, thanks button");
    // call method whatever u need
}];

[alert addAction:yesButton];
[alert addAction:noButton];

[self presentViewController:alert animated:YES completion:nil];

Rapide

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    let yesButton = UIAlertAction(title: "Yes, please", style: .default, handler: {(_ action: UIAlertAction) -> Void in
        /** What we write here???????? **/
        print("you pressed Yes, please button")
        // call method whatever u need
    })
    let noButton = UIAlertAction(title: "No, thanks", style: .default, handler: {(_ action: UIAlertAction) -> Void in
        /** What we write here???????? **/
        print("you pressed No, thanks button")
        // call method whatever u need
    })
    alert.addAction(yesButton)
    alert.addAction(noButton)
    present(alert, animated: true) { _ in }
35
Anbu.Karthik

Vous devez en fait écrire le code après avoir appuyé sur OK et sur le bouton d'annulation dans leurs blocs d'achèvement respectifs.

UIAlertController * alert=[UIAlertController 

alertControllerWithTitle:@"Title" message:@"Message"preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction* yesButton = [UIAlertAction
                        actionWithTitle:@"Yes, please"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {
                            [self okButtonPressed];

                        }];
   UIAlertAction* noButton = [UIAlertAction
                            actionWithTitle:@"No, thanks"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
                               [self cancelButtonPressed];

                           }];

   [alert addAction:yesButton];
   [alert addAction:noButton];

   [self presentViewController:alert animated:YES completion:nil];


 - (void)cancelButtonPressed{
     // write your implementation for cancel button here.
}

 - (void)okButtonPressed{
    //write your implementation for ok button here
 }
4
Shehzad Ali

Vous pouvez simplement laisser ces blocs nil si vous n'avez pas besoin d'actions supplémentaires après avoir appuyé sur le bouton:

UIAlertAction* yesButton = [UIAlertAction
                        actionWithTitle:@"Yes, please"
                        style:UIAlertActionStyleDefault
                        handler:nil];
0
Yulia