首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有动态按钮列表的action sheet类型的UIAlertViewController

具有动态按钮列表的action sheet类型的UIAlertViewController
EN

Stack Overflow用户
提问于 2015-10-13 18:54:51
回答 5查看 12K关注 0票数 5

由于UIAlertViewUIActionsheet在iOS 8中已被弃用,因此建议不要同时使用这两种旧的动作单方法,我可以借助classes.With循环动态添加选项卡。

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];
}

我可以在委托方法中使用按钮索引来执行适当的action.This,这样我就可以动态地创建动作表。所以对于UIAlertController类,这是如何实现的,我之所以问这个问题,是因为在UIAlertController类中,我们必须添加带有动作块的动作处理程序。

EN

回答 5

Stack Overflow用户

发布于 2015-10-13 19:02:16

就像你在这里做的一样。

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

    [alertController addAction:action];
}

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

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

票数 4
EN

Stack Overflow用户

发布于 2015-10-13 19:34:22

这可以通过下面的方法来完成。

UIAlertController *departureActnSht = [UIAlertController alertControllerWithTitle:@"Departure City"
                                                                          message:@"Select your choice"
                                                                   preferredStyle:UIAlertControllerStyleActionSheet];

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

        self.txtDepartureCity.text = arrayDepartureList[j];
    }];

    [departureActnSht addAction:action];
}

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){

    [self dismissViewControllerAnimated:departureActnSht completion:nil];
}];

[departureActnSht addAction:cancelAction];
[self presentViewController:departureActnSht animated:YES completion:nil];
票数 4
EN

Stack Overflow用户

发布于 2020-03-16 20:48:01

 let arrayList = ["Agra","Banglore","delhi","Mumbai"]
        let optionMenu = UIAlertController(title: nil, message: "Choose City", preferredStyle: .actionSheet)
        for i in arrayList{
            let cityName = UIAlertAction(title: i, style: .default, handler:
            {
                (alert: UIAlertAction!) -> Void in



           print(i)

        })
        optionMenu.addAction(cityName)

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:
    {
        (alert: UIAlertAction!) -> Void in
        print("Cancelled")
    })
    optionMenu.addAction(cancelAction)
    self.present(optionMenu, animated: true, completion: nil
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33100823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档