如何在UIAlertController中添加按钮在IOS 9中

时间:2016-02-02 11:48:25

标签: ios objective-c iphone uialertcontroller

我们如何在iOS 9中使用UIAlertView以及如何在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];

3 个答案:

答案 0 :(得分:35)

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

<强>迅速

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 }

答案 1 :(得分:4)

您确实需要在确认后编写代码,并在各自的完成块中按下取消按钮。

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
 }

答案 2 :(得分:0)

如果您在点击按钮后不需要任何其他操作,则可以保留这些块nil

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