 |
 |
|
 |
09-22-2008, 02:18 PM
|
#1 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 31
|
Handle keyboard "Done" press on text field.
All I really need is the keyboard to close when the "Done" button is clicked on the keyboard window; could someone point me towards how I should do this?
I have searched around on the developer.apple.com/iphone site but I can't find anything; I have also reviewed the functionality in the UICatalog sample app but it displays its textfields as part of a tableview which appears to have different editing events...
Any help is appreciated!
|
|
|
09-22-2008, 02:24 PM
|
#2 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 75
|
you need to return the delegate to the view
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
__________________
Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
|
|
|
09-22-2008, 03:03 PM
|
#3 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 31
|
Do I need to do anything to hook the delegate up to the textbox or just make sure it is available on the view controller which contains the text box?
|
|
|
09-22-2008, 04:04 PM
|
#4 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 75
|
set the delegate = self for the text field property
__________________
Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
|
|
|
09-23-2008, 10:22 AM
|
#5 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 31
|
I pretty much copy and pasted your event handler into my ViewControllerSubClass, I then added a text box in Interface Builder and bound it to a UITextField outlet property. I set the myTextField.delegate = self in the ViewController.initWIthNibName method. Everything seems to get executed and go without a hitch but it still doesn't close the keyboard when I click done...
Also I get a warning at the line where I assign the delegate saying "warning class 'MyViewController' does not implement the 'UITextFieldDelegate' protocol"... any idea how to make this warning go away?
|
|
|
09-23-2008, 10:53 AM
|
#6 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 75
|
well you need to tell your class it will be a delegate. So for example
Code:
@interface OptionsAccountCell : UITableViewCell <UITextFieldDelegate> {
You are missing that in your .h file
__________________
Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
|
|
|
09-23-2008, 10:54 AM
|
#7 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 157
|
Quote:
Originally Posted by shawn
Also I get a warning at the line where I assign the delegate saying "warning class 'MyViewController' does not implement the 'UITextFieldDelegate' protocol"... any idea how to make this warning go away?
|
Make your view controller implement that protocol would do it :P
Your header should look like
Code:
@interface MyViewController : UIViewController <UITextFieldDelegate>
The HelloWorld example shows how to do this.
|
|
|
10-02-2008, 08:14 AM
|
#8 (permalink)
|
|
New Member
Join Date: Oct 2008
Posts: 2
|
 I thought i need only to setup an empty callback (ie IBAction) for "Did End On Exit" event of the UITextField (as IBOutlet) in the InterfaceBuilder!
Like this:
//header
@interface MyView : UIView
{
IBOutlet UITextField *name;
}
And the keyboard will be hidden after editing is complete.
And the keyboard will be hidden after editing ends.
- (IBAction)doneButtonOnKeyboardPressed: (id)sender;
//m-file
@implementation MyView
- (IBAction)doneButtonOnKeyboardPressed: (id)sender
{}
@end
Last edited by incorrect.user; 10-02-2008 at 08:16 AM.
|
|
|
07-16-2009, 05:32 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 4
|
Quote:
Originally Posted by incorrect.user
 I thought i need only to setup an empty callback (ie IBAction) for "Did End On Exit" event of the UITextField (as IBOutlet) in the InterfaceBuilder!
Like this:
//header
@interface MyView : UIView
{
IBOutlet UITextField *name;
}
And the keyboard will be hidden after editing is complete.
And the keyboard will be hidden after editing ends.
- (IBAction)doneButtonOnKeyboardPressed: (id)sender;
//m-file
@implementation MyView
- (IBAction)doneButtonOnKeyboardPressed: (id)sender
{}
@end
|
If you haven't sorted it out yet you do the following:
In your .h file type:
Code:
@interface WhatEverViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField * firstTextField;
IBOutlet UITextField * secondTextField;
IBOutlet UITextField * doneButtonPressed;
}
@property (nonatomic, retain) IBOutlet UITextField * firstTextField;
@property (nonatomic, retain) IBOutlet UITextField * secondTextField;
@property (nonatomic, retain) IBOutlet UITextField * doneButtonPressed;
@end
In your .m file add the following: (replace with your own info)
Code:
@synthesize firstTextField, secondTextField, doneButtonPressed;
- (BOOL)textFieldShouldReturn:(UITextField *)doneButtonPressed {
NSLog(@"Keyboard Done Pressed");
[doneButtonPressed resignFirstResponder];
return YES;
}
In you nib file select your UITextField and make the delegate the FilesOwner, do this for all TextFields and the keyboard will hide when the done button is pressed.
|
|
|
08-31-2009, 10:33 PM
|
#10 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 1
|
What if you don't want the keyboard to pop-up once the first field is selected as a first responder? Basically, what if you don't want a particular field to be the first responder, but you want the Done button functionality?
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 495 |
| 48 members and 447 guests |
| Abdel, ackpth, activ8, AjohnB, Alchemda, BostonMerlin, Centurion Games, cjamerlan, CoolApps, coolman, Corey, dany88, dbarrett, dda, designomatt, dmf1978, gonk, greenuns, Gudus, imsatasia, IphoneSdk, jharrah, Kalimba, LemonMeringue, Link, markbuchanan, MiniRobinho, montage, msudan, naomipunkclan, NeilB, nibby, Noise, P2k, pashik, saul102, simpsonaty, Snappy, StefanL, svguerin3, Tambourin, the1nz4ne, treazer, TunaNugget, Tuszy, victorsk, williamlegate, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,510
Threads: 35,793
Posts: 156,803
Top Poster: smasher (2,449)
|
| Welcome to our newest member, dmf1978 |
|