Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertViewController of type action sheet with dynamic list of buttons

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.

like image 604
Vaibhav Avatar asked Oct 13 '15 10:10

Vaibhav


5 Answers

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

like image 104
Mohamed Sayed Avatar answered Oct 04 '22 06:10

Mohamed Sayed


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];
like image 36
Vaibhav Avatar answered Oct 01 '22 06:10

Vaibhav


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/

like image 20
dirtydanee Avatar answered Oct 02 '22 06:10

dirtydanee


 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
like image 25
Govind singh Avatar answered Oct 04 '22 06:10

Govind singh


Swift 4

UIAlertController with dynamic UIAlertAction

  • Create an array of String for buttons title
let buttonTitles = ["First", "Second", "Third"]
  • Closure
typealias completionWithValueAndIndex = (_ actionTitle :String, _ index: Int) ->()
  • Define a method 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)
 }
  • Use the method
showAlertsList(title: "Test title", message: "Test message", buttonsTitle: buttonTitles) { actionTitle, index in
     print(actionTitle)
     print(index)
}
like image 38
Rashid Latif Avatar answered Oct 04 '22 06:10

Rashid Latif