Quote:
Originally Posted by cowgod2007
Hello,
So I have a simple text view. I want it so that when the user types anything and presses "Done", the stuff they typed will be saved into a global variable. First off, how do I make a "Done" button on the keyboard or even make it go away? What is the code? And also, how do I save the data that was typed into the text field?
Thanks!
|
Making the keyboard retract, I can help you with. Saving data - I'm not there yet, so maybe someone else can point you in the right direction.
Presumably, you'll want the keyboard to retract when either the user taps "Done" on the keyboard, or taps the background.
When the user taps "Done"
You'll need an outlet for the UITextField and two IBAction methods in you nib's controller class. We'll use:
Code:
UITextField *textField;
Code:
- (IBAction)textFieldDoneEditing:(id)sender;
and
Code:
- (IBAction)backgroundTap;
I don't know what level you're at, but I'm assuming you know how to define these in the correct places.
Code:
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
Code:
- (IBAction)backgroundTap {
[textField resignFirstResponder];
//If you had any other UITextFields, you would tell them to resignFirstResponser as well. There's no harm in telling an object to resign first responder status if it doesn't hold it.
}
In your nib, connect the UITextField to the new outlet. Then connect the DidEndOnExit event to the textFieldDoneEditing action in File's Owner.
Next, you need to set the Return Key option of the text input traits for the UITextField to "Done". This will make the standard keyboard return key a "Done" key.
When the user taps the background
You need to change the class of the UIView to UIControl. UIControl is a subclass of UIView, so everything else will work, but you will be able to link events on the view to actions in your controller class. Connect the Touch Down event to the backgroundTap event in File's Owner.
After changing the class of the original UIView, you may need to reconnect it to the view outlet in File's Owner.
Saving the TextField to a variable
I assume you want to do more with the entered text than just store it to a temporary variable, so I'll not comment here as I'd be out of my depth.
HTH