我正在将我的代码从使用 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/