As UIAlertView
and UIActionsheet
is deprecated from iOS 8.So it is advised not to use both of these two classes.With old action sheet method I could able to add tabs dynamically with the help of 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];
}
and I can use button index in delegate method to perform appropriate action.This way I can create action sheet on the fly. So with UIAlertController
class how this can be achieved,I am asking this because in UIAlertController
class we have to add action handlers with their action block.
Since UIActionSheet
is deprecated in iOS 8, you must use UIAlertViewController
and add every action as this example:
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];
According to your example you can add UIAlertAction
in the for loop and the action leads to some function that handles the selection.
Another Solution i prefer: Use ActionSheetPicker, to select your departure cities from a picker instead of a button for each city you can find it here: https://github.com/skywinder/ActionSheetPicker-3.0
It can be done by following way..
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];
Just do the same like you did here.
for (int j =0 ; j<arrayDepartureList.count; j++)
{
NSString *titleString = arrayDepartureList[j];
UIAlertAction * action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
}
if you need custom action for every action, you can define them in the handler section.
For further info, take a look here: http://hayageek.com/uialertcontroller-example-ios/
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
Swift 4
UIAlertController
with dynamic UIAlertAction
String
for buttons titlelet buttonTitles = ["First", "Second", "Third"]
typealias completionWithValueAndIndex = (_ actionTitle :String, _ index: Int) ->()
showAlertsList
func showAlertsList(title: String, message: String, buttonsTitle:[String], completion: completionWithValueAndIndex? = nil) {
let alert = UIAlertController( title: title, message:message, preferredStyle: .alert)
let handler = { (action: UIAlertAction) -> Void in
let index = alert.actions.firstIndex { $0 == action}
completion?(action.title ?? "", index ?? 0)
}
for i in 0..<buttonsTitle.count {
alert.addAction(UIAlertAction(title: buttonsTitle[i], style: .default, handler: handler))
}
self.present(alert, animated: true, completion: nil)
}
showAlertsList(title: "Test title", message: "Test message", buttonsTitle: buttonTitles) { actionTitle, index in
print(actionTitle)
print(index)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With