2015-11-19 3 views
2

Я конвертирую свой код с помощью UIActionSheet для использования UIAlertController.Как добавить динамические кнопки в UIAlertController?

Как я сделать это с помощью UIActionSheet, как это:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Gender" 
                 delegate:self 
               cancelButtonTitle:nil 
              destructiveButtonTitle:nil 
               otherButtonTitles:nil]; 
for (NSDictionary *genderInfo in self.genderList) { 
    NSString *gender = [[genderInfo objectForKey:@"description"] capitalizedString]; 
    [actionSheet addButtonWithTitle:gender]; 
} 
[actionSheet addButtonWithTitle:@"Cancel"]; 

И только обрабатывать то, что кнопка нажата на метод делегата от действия листа.

При преобразовании его в контроллер предупреждений я заметил, что на каждом из действий предупреждения есть обработчик. Интересно, как я буду использовать контроллер предупреждений, чтобы иметь динамические кнопки, с которыми я могу справиться.

+0

и может добавить uialertaction * button1 ....... это то, что у ¨R ищет? –

+0

В чем именно ваш вопрос? Вы добавляете 'UIAlertAction' для каждой кнопки, которую вы хотите на листе. Есть ли какой-то аспект API, который запутывает? – Avi

+0

Мой вопрос, если значения кнопок указаны в списке, как я могу их добавить и обработать действия динамически? – dizzyboy

ответ

5

Понял!

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Gender:" 
                     message:nil 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 
for (NSDictionary *genderInfo in self.genderList) { 
    NSString *gender = [[genderInfo objectForKey:@"description"] capitalizedString]; 
    UIAlertAction *action = [UIAlertAction actionWithTitle:gender 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction *action) { 
                 NSString *title = action.title; 
                 //you can check here on what button is pressed using title 
                }]; 
    [alertController addAction:action]; 
} 
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
                 style:UIAlertActionStyleCancel 
                handler:^(UIAlertAction *action) { 
                }]; 
[alertController addAction:cancelAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 
5

Здесь я упомянуть код массива нагрузки в UIAlertController динамически с изображением & текстом: Output Image here

NSArray *numbersArrayList = @[@"One", @"Two", @"Three", @"Four", @"Five", @"Six"]; 
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Numbers:" 
                     message:nil 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 
for (int j =0 ; j<numbersArrayList.count; j++){ 
    NSString *titleString = numbersArrayList[j]; 
    UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     NSLog(@"Selected Value: %@",numbersArrayList[j]); 

    }]; 
    [action setValue:[[UIImage imageNamed:@"USA16.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; 
    [alertController addAction:action]; 
} 
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
                 style:UIAlertActionStyleCancel 
                handler:^(UIAlertAction *action) { 
                }]; 
[alertController addAction:cancelAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 
+0

Хороший! Как насчет того, хочу ли я загружать разные изображения? – JUL2791