2014-11-25 5 views
1

Мне нужно получить текст из текстового поля в alertcontroller как NSString, но когда я пытаюсь вернуть то, что я получаю ошибку «Несовместимые типы указателей блока отправки NSString»Ошибка при получении строки из AlertController TextField

I может получить значение и показать его в NSLog внутри обработчика, но не за его пределами.

UIAlertAction *enterAction = [UIAlertAction 
           actionWithTitle:@"Enter" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction *action) { 
            NSLog(@"Enter Action"); 
            NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text; 
            NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text; 
            [alertController dismissViewControllerAnimated:YES completion:nil]; 
            NSLog(@"%@", name); 
            NSLog(@"%@", time); 
            return name; 
           }]; 

Полный кусок кода здесь

- (void)textPopUp{ 

    UIAlertController * alertController = [UIAlertController 
             alertControllerWithTitle:@"Attention!" 
             message:@"Please enter project name, and time spent below" 
             preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *cancelAction = [UIAlertAction 
           actionWithTitle:@"Cancel" 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction *action){ 
            [alertController dismissViewControllerAnimated:YES completion:nil]; 
            NSLog(@"Cancel Action"); 
           }]; 

    UIAlertAction *enterAction = [UIAlertAction 
           actionWithTitle:@"Enter" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction *action) { 
            NSLog(@"Enter Action"); 
            NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text; 
            NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text; 
            [alertController dismissViewControllerAnimated:YES completion:nil]; 
            NSLog(@"%@", name); 
            NSLog(@"%@", time); 
            return name; 
           }]; 

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = NSLocalizedString(@"Name", @"Name"); 
    }]; 

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = NSLocalizedString(@"Time", @"Time"); 
    }]; 

    [alertController addAction:cancelAction]; 
    [alertController addAction:enterAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
} 

ответ

0

Эта ошибка показывает, потому что в обработчике UIAlertAction вы возвращаете строку. Вы можете установить textField текст на контроллер следующим образом:

__weak TableViewController *weakSelf = self; 

UIAlertAction *enterAction = [UIAlertAction 
           actionWithTitle:@"Enter" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction *action) { 
            NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text; 
            NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text; 
            [alertController dismissViewControllerAnimated:YES completion:nil]; 
            weakSelf.result = name; 
           }]; 
Смежные вопросы