ios - 具有动态按钮列表的操作表类型的 UIAlertViewController

标签 ios ios8 uialertcontroller

由于 UIAlertViewUIActionsheet 已从 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/

相关文章:

ios - IOS 上的 Hpple XPath 查询问题

ios8 - IOS 8 上的 Iphone Simulator Xcode 6 可视化两个黑条

swift - 连接 Swift 自定义键盘中的按钮时出错

ios - 如何消除 UIAlertController 的延迟?

ios - 如何或可以在函数的参数中传递结构?

ios - 经批准的应用程序内购买应用程序无法正常工作

ios - 如何让新项目显示在 ListView 中

ios - 将未知格式(任何格式)的字符串转换为日期

iphone - 创建一个仅包含 TextView /字符串和按钮 SWIFT ios 8 的弹出窗口

swift - UIAlertController - 操作顺序