如何在UIAlertController中添加按钮在IOS 9中 [英] how to add button in UIAlertController In IOS 9

查看:107
本文介绍了如何在UIAlertController中添加按钮在IOS 9中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在iOS 9中使用 UIAlertView 以及如何在 UIAlertController中添加按钮

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


推荐答案

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 }

这篇关于如何在UIAlertController中添加按钮在IOS 9中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆