【问题标题】:how to add button in UIAlertController In IOS 9如何在 iOS 9 中的 UIAlertController 中添加按钮
【发布时间】:2016-02-02 11:48:25
【问题描述】:

我们如何在 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];

【问题讨论】:

  • 你已经添加了两个按钮,在那个//我们在这里写什么????????,你需要处理你需要的任何动作,就是这样所有兄弟,你需要添加更多按钮..
  • 那么如何处理那个动作???我不明白:(@Anbu.Karthik

标签: ios objective-c iphone uialertcontroller


【解决方案1】:
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 }

【讨论】:

  • 首先按下按钮放置断点并检查,NSlog 将被调用,这里不需要 UIAlertcontroller 上的任何代表,如果您使用 alertview,代表将被调用,我们处理进一步的过程但是在 UIAlertController 中不需要委托方法,UIAlertaction 是处理每个按钮的委托动作。
  • @anupgupta -- 很高兴听到这个兄弟,最初我也遇到过这个问题,一天一天我像你一样学习自己......
  • @Anbu.Karthik,我的 UIAlertController 是全局变量,因为我有进度条并对其进行更新,但问题是单击 UIAlertAction 按钮时它会崩溃。
【解决方案2】:

您实际上需要在各自的完成块中按下 ok 和 cancel 按钮后编写代码。

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
 }

【讨论】:

  • 谢谢兄弟@anupgupta
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    相关资源
    最近更新 更多