如何在 UIAlertController 上添加动态按钮?

Posted

技术标签:

【中文标题】如何在 UIAlertController 上添加动态按钮?【英文标题】:How to add dynamic buttons on UIAlertController? 【发布时间】:2015-11-19 20:22:20 【问题描述】:

我正在将我的代码从使用 UIActionSheet 转换为使用 UIAlertController。

我使用 UIActionSheet 的方式是这样的:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Gender"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];
for (NSDictionary *genderInfo in self.genderList) 
    NSString *gender = [[genderInfo objectForKey:@"description"] capitalizedString];
    [actionSheet addButtonWithTitle:gender];

[actionSheet addButtonWithTitle:@"Cancel"];

只需处理在操作表的委托方法上按下的按钮。

在将其转换为警报控制器时,我注意到每个警报操作都有一个处理程序。我想知道如何实现警报控制器以具有可以处理操作的动态按钮。

【问题讨论】:

你可以添加 uialertaction *button1.......那是你要找的吗? 您的问题到底是什么?您为工作表上所需的每个按钮添加UIAlertAction。 API 是否存在令人困惑的方面? 我的问题是按钮的值是否来自列表,如何添加它们并动态处理操作? 【参考方案1】:

这里我提到了将数组动态加载到 UIAlertController 中的代码:Output Image here

 NSArray *numbersArrayList = @[@"One", @"Two", @"Three", @"Four", @"Five", @"Six"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Numbers:"
                                                                         message:nil
                                                                  preferredStyle:UIAlertControllerStyleActionSheet];
for (int j =0 ; j<numbersArrayList.count; j++)
    NSString *titleString = numbersArrayList[j];
    UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
        NSLog(@"Selected Value: %@",numbersArrayList[j]);

    ];
    [action setValue:[[UIImage imageNamed:@"USA16.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
    [alertController addAction:action];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction *action) 
                                                     ];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

【讨论】:

好!如果我想加载不同的图像呢??【参考方案2】:

知道了!

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Gender:"
                                                                         message:nil
                                                                  preferredStyle:UIAlertControllerStyleActionSheet];
for (NSDictionary *genderInfo in self.genderList) 
    NSString *gender = [[genderInfo objectForKey:@"description"] capitalizedString];
    UIAlertAction *action = [UIAlertAction actionWithTitle:gender
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) 
                                                       NSString *title = action.title;
                                                       //you can check here on what button is pressed using title
                                                   ];
    [alertController addAction:action];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction *action) 
                                                     ];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

【讨论】:

【参考方案3】:

在 Swift 中:

import Foundation
import UIKit

class AlertUtility 
static let CancelButtonIndex = -1;
class func showAlert(_ onController:UIViewController, title:String?,message:String? ) 
    showAlert(onController, title: title, message: message, cancelButton: "OK", buttons: nil, actions: nil)


/**
 - parameter title:        title for the alert
 - parameter message:      message for alert
 - parameter cancelButton: title for cancel button
 - parameter buttons:      array of string for title for other buttons
 - parameter actions:      action is the callback which return the action and index of the button which was pressed
 */

class func showAlert(_ onController:UIViewController?, title:String?,message:String? = nil ,cancelButton:String = "OK",buttons:[String]? = nil,actions:((_ alertAction:UIAlertAction,_ index:Int)->())? = nil) 
    // make sure it would be run on  main queue
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

    let action = UIAlertAction(title: cancelButton, style: UIAlertAction.Style.cancel)  (action) in
        alertController.dismiss(animated: true, completion: nil)
        actions?(action,CancelButtonIndex)
    
    alertController.addAction(action)
    if let _buttons = buttons 
        for button in _buttons 
            let action = UIAlertAction(title: button, style: .default)  (action) in
                let index = _buttons.index(of: action.title!)
                actions?(action,index!)
            
            alertController.addAction(action)
        
    
    if let onController = onController
        onController.present(alertController, animated: true, completion: nil)
    else
        //            let appdel = UIApplication.shared.delegate as! AppDelegate
        //            appdel.window!.rootViewController?.present(alertController, animated: true, completion: nil)
    



在您的 VC 中使用:

//MARK:- IB-ACTION(S)
@IBAction func actionShowAlertButtonTapped(_ sender: Any) 
    AlertUtility.showAlert(self, title: "Are you sure you want to start this order ?", cancelButton: "Cancel", buttons: ["OK", "Button 1", "Button 2", "Button 3", "Button 4","Button 5"], actions: (action, index) -> () in
        if index != AlertUtility.CancelButtonIndex 
            //Cancel Button Action Here..
        else
            //Other Button Actions as per Button Index..
        
    )

【讨论】:

以上是关于如何在 UIAlertController 上添加动态按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UIAlertController 中添加 UIPickerView?

如何在Objective C中通过UIView获取UIAlertController?

如何在 swift 中添加与 UIAlertController 的 textField 一起使用的“基本类型”?

UIAlertController/ActionSheet 在 iPad 上崩溃,如何传递发件人?

如何同时关闭 UIAlertController 和键盘?

Swift 如何获取 UIAlertController 标题高度