由于{8}已弃用UIAlertView
和UIActionsheet
所以建议不要同时使用这两个类。使用旧的操作表方法,我可以在以下帮助下动态添加标签for loop。
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];
}
我可以在委托方法中使用按钮索引来执行适当的操作。这样我就可以动态创建操作表。因此,使用UIAlertController
类可以实现这一点,我问这是因为在UIAlertController
类中我们必须添加动作处理程序及其动作块。
答案 0 :(得分:8)
由于iOS 8中不推荐使用UIActionSheet
,因此您必须使用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
答案 1 :(得分:4)
就像你在这里做的一样。
ga:users
如果您需要为每个操作执行自定义操作,则可以在处理程序部分中定义它们。
有关详细信息,请查看此处: http://hayageek.com/uialertcontroller-example-ios/
答案 2 :(得分:4)
可以通过以下方式完成..
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];
答案 3 :(得分:0)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
showAlertWith(msg: "when to start:", btnTitles:
[
"Now",
"15 secs",
"30 secs",
"1 min.",
])
}
class TaggedAlertAction: UIAlertAction{
var tag = 0
}
final private func showAlertWith(msg: String, btnTitles: [String]){
let alert = UIAlertController(
title: title,
message:msg,
preferredStyle: .alert)
let handler = { (act: TaggedAlertAction) in
let ta = act as! TaggedAlertAction
let tag = ta.tag
print(tag)
}
for i in 0..<btnTitles.count {
let a = TaggedAlertAction(title: btnTitles[i], style: .default, handler: handler
a.tag = i+1 // or Whatever number You need...
alert.addAction(a)
}
self.present(alert, animated: true, completion: nil)
}
}
答案 4 :(得分:0)
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
答案 5 :(得分:-1)
您可以根据任何动态列表向UIAlertViewControler添加操作。使用一个简单的for循环并在每次迭代中创建UIAction。最好根据您的定义标记设置标记值并执行操作,以下是示例代码。希望能帮助到你 https://github.com/Usman4Whizpool/UIAlertController