2015-08-10 1 views
0

Я создал лист действий с помощью Objective-C. Когда я хочу получить название выбранной кнопки, я застрял. Пожалуйста, помогите мне. (Я знаю, что есть метод делегирования для получения названий действий, но он устарел в iOS 8.0+.) Это я мой код. Мое требование - получить выбранный заголовок и обновить его до текстового поля.Получить выбранные названия кнопок из листа UIAlertAction, добавленного в UIAlertViewController

NSArray *subjectType = [NSArray arrayWithObjects:@"Customer Service",@"Product Info",@"Deals",@"Special",@"Others", nil]; 

    UIAlertController *subjectSheet = [UIAlertController alertControllerWithTitle:@"Subjects" message:@"Select a prefered subject" preferredStyle:UIAlertControllerStyleActionSheet]; 


    for (int i=0; i<subjectType.count; i++) { 
     UIAlertAction *myalertAction = [UIAlertAction actionWithTitle:[subjectType objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

     }]; 


    [subjectSheet addAction:myalertAction]; 
} 
+0

класс 'UIActionSheet' сам был устаревшим iOS 8. Пожалуйста, покажите нам свой код. – ozgur

+0

Какой смысл получить титул? Если бы вы могли объяснить, чтобы мы могли правильно ответить –

+0

@ozgur, я редактировал свой вопрос. вы можете увидеть мой код – madhev

ответ

1

Попробуйте название свойства UIAlertAction

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title" 
                   message:@"message" 
                 preferredStyle:UIAlertControllerStyleActionSheet]; 
[alert addAction:[UIAlertAction actionWithTitle:@"OK" 
              style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
               NSLog(@"%@",action.title); 
              }]]; 
[self presentViewController:alert animated:true completion:nil]; 
1

Вы можете сделать некоторые вещи, как показано ниже, чтобы осуществить то, что вы хотели бы достичь

NSArray *subjectType = [NSArray arrayWithObjects:@"Customer Service",@"Product Info",@"Deals",@"Special",@"Others", nil]; 

UIAlertController *subjectSheet = [UIAlertController alertControllerWithTitle:@"Subjects" message:@"Select a prefered subject" preferredStyle:UIAlertControllerStyleActionSheet]; 


for (int i=0; i<subjectType.count; i++) { 
    UIAlertAction *myalertAction = [UIAlertAction actionWithTitle:[subjectType objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
      [self takeActionAccordingToString:action.title]; 
    }]; 


[subjectSheet addAction:myalertAction]; 
[self presentViewController:subjectSheet animated:YES completion:nil]; 
} 
-(void)takeActionAccordingToString:(NSString *)actionTitle{ 
if([actionTitle isEqualToString:[yourArray objectAtIndex:0]]){ 
//you can take action here 
} 
if([actionTitle isEqualToString:[yourArray objectAtIndex:1]]){ 
//you can take action here 
} 
if([actionTitle isEqualToString:[yourArray objectAtIndex:2]]){ 
//you can take action here 
} 
if([actionTitle isEqualToString:[yourArray objectAtIndex:3]]){ 
//you can take action here 
} 
if([actionTitle isEqualToString:[yourArray objectAtIndex:4]]){ 
//you can take action here 
} 
//etc.... 
} 
+0

большое спасибо моим дорогим – madhev

Смежные вопросы