Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add button in UIAlertController In IOS 9

how we use UIAlertView in iOS 9 and how to add button in 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];
like image 335
Anup Gupta Avatar asked Feb 02 '16 11:02

Anup Gupta


2 Answers

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

swift

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 }
like image 102
Anbu.Karthik Avatar answered Oct 22 '22 18:10

Anbu.Karthik


You actually need to write code after ok and cancel button pressed in their respective completion blocks.

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
 }
like image 39
Shehzad Ali Avatar answered Oct 22 '22 20:10

Shehzad Ali