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

diz*_*boy 3 objective-c ios 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"];
Run Code Online (Sandbox Code Playgroud)

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

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

Man*_*mam 8

在这里,我使用图像和文本动态地将负载数组的代码提到UIAlertController:输出图像

 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];
Run Code Online (Sandbox Code Playgroud)


diz*_*boy 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];
Run Code Online (Sandbox Code Playgroud)