由于UIAlertView
和UIActionsheet
在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
类中,我们必须添加带有动作块的动作处理程序。
发布于 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/
发布于 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];
发布于 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
https://stackoverflow.com/questions/33100823
复制相似问题