0

I'm trying to learn Objective C and iOS development, but am having trouble. I am trying to create a very basic app that has a text field and that prints out the value entered into the text field using NSLog when the return key is pressed. I have added the text field via storyboard, but am unsure how to create the delegate method needed to make this work. I have thus far added the following code to the ViewController.h file: @property (weak, nonatomic) IBOutlet UITextField *textfld;

Thanks in advance for any help.

2

1 Answer 1

0

Set the delegate of the UITextField outlet to be your ViewController. Then have your ViewController implement the UITextFieldDelegate protocol. Then in the textFieldDidEndEditing: method, print out the text property of the textView input.

Update

- (void)viewDidLoad {
    [super viewDidLoad];
    self.txtField.delegate = self;
}
13
  • Hi - how can I check whether I have set the delegate of the UITextfield outlet to be the ViewController? I think I have done this already, but am not sure tbh Apr 12, 2014 at 3:23
  • @user3525783 Questions? Apr 12, 2014 at 3:46
  • Add this. @interface ViewController () <UITextFieldDelegate> Then implement textFieldDidEndEditing. Here's the docs. developer.apple.com/library/ios/documentation/uikit/reference/…: Apr 12, 2014 at 3:56
  • FYI UITextViewDelegate has an even better method if you want to see the NSLog any time the text changes. textViewDidChange. But you need to use a UITextView instead of a UITextField. Apr 12, 2014 at 3:59
  • Hi - so I think I have implemented your comments, but the text is not showing up. Can you advise? I have added - (void)textFieldDidEndEditing:(UITextField *)textfld { NSLog(@"text is : %@",textfld.text); } Do i need something more to get it to display when the return key is pressed? Apr 14, 2014 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.