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

Vai*_*hav 5 ios ios8 uialertcontroller

作为UIAlertViewUIActionsheet从iOS版8.So过时,建议不要同时使用这两种classes.With老动作片的方法,我可以能够添加标签动态地的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];
}
Run Code Online (Sandbox Code Playgroud)

我可以在委托方法中使用按钮索引来执行适当的操作.这样我就可以动态创建动作表.所以在UIAlertController课堂上如何实现这一点,我问这个,因为在UIAlertController课堂上我们必须添加动作处理程序及其动作块.

小智 8

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

根据您的示例,您可以添加UIAlertActionfor循环,并且操作会导致一些处理选择的函数.

我更喜欢的另一个解决方案: 使用ActionSheetPicker,从选择器中选择您的出发城市而不是每个城市的按钮,您可以在此处找到它:https: //github.com/skywinder/ActionSheetPicker-3.0


dir*_*nee 4

就像你在这里做的那样。

for (int j =0 ; j<arrayDepartureList.count; j++)
{
    NSString *titleString = arrayDepartureList[j];
    UIAlertAction * action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:action];
}
Run Code Online (Sandbox Code Playgroud)

如果您需要为每个操作自定义操作,您可以在处理程序部分定义它们。

有关更多信息,请查看此处: http://hayageek.com/uialertcontroller-example-ios/