由于 UIAlertView
和 UIActionsheet
已从 iOS 8 中弃用。因此建议不要同时使用这两个类。使用旧的操作表方法我可以添加在 for 循环的帮助下动态选择标签。
UIActionSheet *actionSheet = [[UIActionSheet alloc]init];
actionSheet.title = @"Departure City";
actionSheet.delegate = self;
actionSheet.tag = 1;
for (int j =0 ; j<arrayDepartureList.count; j++)
{
NSString *titleString = arrayDepartureList[j];
[actionSheet addButtonWithTitle:titleString];
}
我可以在委托(delegate)方法中使用按钮索引来执行适当的操作。这样我就可以即时创建操作表。所以对于 UIAlertController
类如何实现这一点,我问这个是因为在 UIAlertController
类中我们必须添加操作处理程序及其操作 block 。
最佳答案
由于 UIActionSheet
在 iOS 8 中已弃用,您必须使用 UIAlertViewController
并添加每个操作,如本例所示:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an action sheet."
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"one"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"You pressed button one");
}];
UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"two"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"You pressed button two");
}];
[alert addAction:firstAction];
[alert addAction:secondAction];
[self presentViewController:alert animated:YES completion:nil];
根据您的示例,您可以在 for 循环中添加 UIAlertAction
,该操作会导致某个处理选择的函数。
我更喜欢的另一种解决方案: 使用 ActionSheetPicker , 从选择器中选择您的出发城市而不是每个城市的按钮,您可以在这里找到它: https://github.com/skywinder/ActionSheetPicker-3.0
关于ios - 具有动态按钮列表的操作表类型的 UIAlertViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33100823/