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

标签 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"];

并且只处理在 action sheet 的委托(delegate)方法上按下了什么按钮。

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

最佳答案

这里我提到了使用图像和文本将数组动态加载到 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];

关于ios - 如何在 UIAlertController 上添加动态按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813669/

相关文章:

ios - 如何管理UIButton单击声音iOS

ios - 点击手势识别器 - 哪个对象被点击了?

ios - 每一个新行添加一个编号列表 UITextView

objective-c - -dealloc 对于从 KVO 取消注册来说太晚了

ios - UIAlertController 的 UIAlertView show() 行为

ios - 有没有办法在 Swift 中将 UIPickerView 添加到 UIAlertController(Alert 或 ActionSheet)中?

ios - UIAlertController 的按钮在单击时更改其颜色。怎么解决? - swift

iOS - 在 Documents 文件夹中创建一个新目录

c# - mac用户名和密码怎么找

ios - 当 View 已向上移动键盘时,UINavigationBar BarButtonItems 不响应点击