Mike Peat
Mike Peat

Reputation: 485

Detecting iOS UI events in programmatically created controls

I want to have a text input (UITextField) which I create in the main View Controller react to the user's input (specifically when they tap the keyboard's "return" key), but I can't find any way to do this.

I understand that the "usual" way to do this is to create the control in Interface Builder and use that to connect events on the control to exposed IBAction methods, but I want to do this without Interface Builder.

Alternatively

I can create the control in Interface Builder and have its events connected normally, but how do I then get ahold of the object to control it (I just want to be able to hide/show it and perhaps reposition in) from my code?

Again I can't find a way to do this

Either route would work for me - I really don't care how I create the UITextField.

TIA

Mike Peat

Upvotes: 2

Views: 733

Answers (2)

arnoapp
arnoapp

Reputation: 2486

You need to set up a delegate for the TextField.

textField.delegate = self;

The delegate has certain methodes that reacts to actions with the textField.

You can react to the UserInput with the delegate methode:

- (void)textFieldDidEndEditing:(UITextField *)textField

It is called after the DoneKey (or whatever key you use to end the editing) is pressed

Hope this is what you are looking for

Upvotes: 1

Kai Huppmann
Kai Huppmann

Reputation: 10775

You could add an observer to the notification center like this:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];

and implement the corresponding method:

-(void) keyPressed: (NSNotification*) notification
{
  NSLog([[notification object]text]);
}

Upvotes: 1

Related Questions