Search code examples
iosobjective-cuitextfieldnslog

NSLog Print when Enter Key is Pressed


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. The code is below. So far, nothing seems to happen when text is put into the text field and I press enter. Can anyone assist?

@property (weak, nonatomic) IBOutlet UITextField *textfld;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textfld.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)textFieldDidBeginEditing:(UITextField *)textfld {

}


- (void)textFieldDidEndEditing:(UITextField *)textfld {

    NSLog(@"text is : %@",textfld.text);

}

http://pastie.org/9078792

http://pastie.org/9078793


Solution

  • Use - (BOOL)textFieldShouldReturn:(UITextField *)textField delegate method of UITextField in ViewController.m

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        NSLog(@"text is : %@",textfld.text);
        return YES:
    }
    

    textFieldShouldReturn: asks the delegate if the text field should process the pressing of the return button. Read more.