2014-12-29 8 views
5

Próbuję uzyskać tekst z pola tekstowego UIAlertController. Czy ktoś może mi powiedzieć, co zrobiłem źle, ponieważ nie działa. Dostaję zwrot NIL.UIAlertController uzyskać tekst

- (IBAction)btnMakeRec { 

     UIAlertController *alert= [UIAlertController 
            alertControllerWithTitle:@"Recipe Name?" 
            message:@"" 
            preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action){ 


               UITextField *temp = alert.textFields.firstObject; 

               RecipeDesc.text = temp.text; 
               // HERE temp is Nil 


               RDescription = [[NSString alloc ] initWithFormat:@"%@", RecipeDesc.text]; 


                }]; 

     UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                 // NSLog(@"cancel btn"); 

                  [alert dismissViewControllerAnimated:YES completion:nil]; 

                 }]; 

     [alert addAction:ok]; 
     [alert addAction:cancel]; 

     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Enter the name of the recipe"; 
      textField.keyboardType = UIKeyboardTypeDefault; 
     }]; 

     [self presentViewController:alert animated:YES completion:nil]; 

    } 

} 
+0

Spróbuj wstawić 'NSLog (@" alert.textFields:% @ ", alert.textFields);' w tym samym bloku. – bauerMusic

+0

OK Zrobiłem NSLog i otrzymałem: alert.textFields: ( "<_UIAlertControllerTextField: 0x7b941230; frame = (4 4; 229.333 16); text =" yyyy "; clipsToBounds = YES; opaque = NO; gestRecognizers = ; layer = > " ) Zobacz tekst =" rrrr ", to jest to, co wpisałem. Jak zdobyć ten tekst na mój ciąg? –

Odpowiedz

28

Oto 'czysty copy/paste' wersja:

Swift 3:

let alert = UIAlertController(title: "Alert Title", 
           message: "Alert message", 
           preferredStyle: UIAlertControllerStyle.alert) 

let ok = UIAlertAction(title: "OK", 
         style: UIAlertActionStyle.default) { (action: UIAlertAction) in 

         if let alertTextField = alert.textFields?.first, alertTextField.text != nil { 

          print("And the text is... \(alertTextField.text!)!") 

         } 


} 

let cancel = UIAlertAction(title: "Cancel", 
          style: UIAlertActionStyle.cancel, 
          handler: nil) 

alert.addTextField { (textField: UITextField) in 

    textField.placeholder = "Text here" 

} 

alert.addAction(ok) 
alert.addAction(cancel) 

self.present(alert, animated: true, completion: nil) 

Swift 2:

let alert = UIAlertController(title: "Alert Title", 
           message: "Alert message", 
           preferredStyle: UIAlertControllerStyle.Alert) 

let ok = UIAlertAction(title: "OK", 
         style: UIAlertActionStyle.Default) { (action: UIAlertAction) in 

         if let alertTextField = alert.textFields?.first where alertTextField.text != nil { 

          print("And the text is... \(alertTextField.text!)!") 

         } 


} 

let cancel = UIAlertAction(title: "Cancel", 
          style: UIAlertActionStyle.Cancel, 
          handler: nil) 

alert.addTextFieldWithConfigurationHandler { (textField: UITextField) in 

    textField.placeholder = "Text here" 

} 

alert.addAction(ok) 
alert.addAction(cancel) 

self.presentViewController(alert, animated: true, completion: nil) 

Cel C:

UIAlertController *alert = [UIAlertController 
          alertControllerWithTitle: @"Alert Title" 
          message: @"Alert message" 
          preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *ok = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault 
              handler:^(UIAlertAction *action){ 


               UITextField *alertTextField = alert.textFields.firstObject; 

               NSLog(@"And the text is... %@!", alertTextField.text); 

              }]; 

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel 
               handler: nil]; 


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 

    textField.placeholder = @"Text here"; 

}]; 

[alert addAction:ok]; 
[alert addAction:cancel]; 

[self presentViewController:alert animated:YES completion:nil]; 

Poprzednia odpowiedź:

Edycja: - Uwaga: Obecnie, oryginalne pytanie wydaje się działać jak jest.

Dla wyjaśnienia:

Instancja UIAlertController powinien trzymać textField w swojej tablicy pól tekstowych po wywołaniu addTextFieldWithConfigurationHandler.

UIAlertController *alertController = ...// Create alert 

// Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController' 

UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) { 

    // alertController.textFields should hold the alert's text fields. 
} 

Jeśli z jakiegoś powodu nie jest, proszę rzucić więcej światła (ponieważ ta kwestia wciąż przyciąga uwagę). Wydaje się (według niektórych komentarzy), że niektórzy ludzie mieli pewne problemy z tym, ale nie dostarczyli informacji poza "to nie działa".


Oryginalny odpowiedź:

Twój kod wygląda dobrze, to powinno działać.

Innym sposobem jest zdefiniowanie UITextField przed UIAlertController *alert=...

UITextField *myTf;

pass textField z addTextFieldWithConfigurationHandler:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Enter the name of the recipe"; 
      textField.keyboardType = UIKeyboardTypeDefault; 

      myTf = textField; 
     }]; 

Następnie, w:

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action){... 
//UITextField *temp = alert.textFields.firstObject; 

// Get the text from your textField 
NSString *temp = myTf.text; 

- EDYCJA

Oryginalny kod plakatu działa teraz tak, jak jest (testowany pod Xcode 7, iOS 9). Mogło być błędem w poprzedniej wersji. Możliwe, że w poprzedniej wersji textField był trzymany przez słabe odniesienie i został zwolniony, chyba że trzymał go inny silny wskaźnik.

+0

który działa. wciąż nie jestem pewien, dlaczego nie działa w drugą stronę. Dzięki za pomoc!! –

+1

To nie działa tak, ponieważ utracisz odniesienie do "alertu" po wyświetleniu tego alertu na ekranie. Powinieneś zachować referencję w globalnej zmiennej twojego ViewController. –

+1

@HoaParis Najpierw ** działa **. Wypróbuj go w czystym projekcie. Obecnie działa dokładnie tak, jak robił to OP (błąd w poprzedniej wersji?). Po drugie, zabawna rzecz w blokach, przechwytują zmienne. – bauerMusic

Powiązane problemy