UIAlertViewController action type with dynamic button list

Since UIAlertViewthey UIActionsheetare deprecated from iOS 8. Therefore, it is recommended not to use both of these two classes. Using the old action method, I could dynamically add tabs using a 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 the button index in the delegate method to perform the appropriate actions. This way I can create an action sheet on the fly. Therefore, with the class UIAlertController, how this can be achieved, I ask about it, because in the class UIAlertControllerwe have to add action handlers with their action block.

+4
source share
4 answers

UIActionSheet iOS 8, 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];

UIAlertAction for, , .

, : ActionSheetPicker, , : https://github.com/skywinder/ActionSheetPicker-3.0

+7

, .

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

.

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

UIAlertViewControler . UIAction . define. . , https://github.com/Usman4Whizpool/UIAlertController

-1

Source: https://habr.com/ru/post/1611405/


All Articles