IOS: How to get text from a text field and display it in a message?

guys. It will sound like I am asking you to do your homework for me, but I don’t know. My employer FINALLY gave me this sweet new MacBook Pro. One of my tasks will include iOS development. I'm pumped up by this and I'm trying to dive into the study, so I make stupid little apps that just let me see how to interact and write code. Today's task is to take the text from the text box and display it as a warning. I searched the Internet many times and found many things - even on StackOverflow, but much of what is on my head is not entirely relevant. So, I hope someone can show me what I did wrong.

Here is my code for the text box:

-(IBAction)showInputMessage:(UITextField *)textField { if ([textField.text isEqualToString:@""]) { return; } UIAlertView *helloEarthInputAlert = [[UIAlertView alloc] initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textField.text] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; // Display this message. [helloEarthInputAlert show]; } 

And then I connect this text box to showInputMessage and run it in the iPhone simulator, but nothing happens when I type in the text and press "Enter".

Thanks in advance. I only played this language from last night.

Jeremiah

+4
source share
2 answers

Set delegate for UITextView.

First declare a delegate:

 @interface YourViewController ()<UITextFieldDelegate> 

Second set for self

 self.textView.delegate = self; 

Use this method:

 -(void)textViewShouldReturn:(UITextField *)textField { if ([textField.text isEqualToString:@""]){ return; } UIAlertView *helloEarthInputAlert = [[UIAlertView alloc] initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textField.text] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; // Display this message. [helloEarthInputAlert show]; } 

To add a property, write the following code in the h file:

  @property (weak, nonatomic) IBOutlet UITextView *textView; 

To connect this text box, go to the storyboard and click on the controller of your kind. Then go to the connection inspector (all the way to the right). Under the exits, drag the circle next to the textView into your text file.

+6
source

Have you UITextField to the xib file or to the storyboard ? Now I see that you have textField as a parameter. You will not get anything like this:

Do it in .h

 IBOutlet UITextField *textFieldTest; 

then tie it in xib or storyboard

  -(IBAction)showInputMessage { if ([textFieldTest.text isEqualToString:@""]) { return; } UIAlertView *helloEarthInputAlert = [[UIAlertView alloc] initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textFieldTest.text] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; // Display this message. [helloEarthInputAlert show]; } 
0
source

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


All Articles