Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone ($4.99)

dotnetIQ ($4.99)

Your First iPhone App ($1.99)

iPocket Tools 9 in 1 ($0.99)

Catch-Me (Free)

Alien Strike ($0.99)

Historic Olympic Medal-Table ($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-22-2008, 02:18 PM   #1 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 31
Default 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!
shawn is offline   Reply With Quote
Old 09-22-2008, 02:24 PM   #2 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 75
Default

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
hijinks is offline   Reply With Quote
Old 09-22-2008, 03:03 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 31
Default

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?
shawn is offline   Reply With Quote
Old 09-22-2008, 04:04 PM   #4 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 75
Default

set the delegate = self for the text field property
__________________
Iphone Noob iPhone Development Blog. Learn to develop for the iPhone SDK with me
hijinks is offline   Reply With Quote
Old 09-23-2008, 10:22 AM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 31
Default

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?
shawn is offline   Reply With Quote
Old 09-23-2008, 10:53 AM   #6 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 75
Default

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
hijinks is offline   Reply With Quote
Old 09-23-2008, 10:54 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 157
Default

Quote:
Originally Posted by shawn View Post
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.
Pring is offline   Reply With Quote
Old 10-02-2008, 08:14 AM   #8 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 2
Default

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.
incorrect.user is offline   Reply With Quote
Old 07-16-2009, 05:32 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 4
Default

Quote:
Originally Posted by incorrect.user View Post
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.
megatron is offline   Reply With Quote
Old 08-31-2009, 10:33 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 1
Default

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?
initself is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 21,510
Threads: 35,793
Posts: 156,803
Top Poster: smasher (2,449)
Welcome to our newest member, dmf1978
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0