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

时间:2015-11-19 20:22:21

标签: ios objective-c uialertcontroller

我正在使用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"];

只需处理代理行动表方法中按下的按钮。

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

3 个答案:

答案 0 :(得分:8)

这里我提到了使用image& amp;动态加载数组到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];

答案 1 :(得分:5)

知道了!

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

答案 2 :(得分:0)

在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..
        }
    })
}