Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
How do I close the keyboard that pops up when a textfield is selected? I don't want to do anything special, I just want to recognize that the done button has been pressed and close the keyboard. Optionally and as a better alternative, being able to use my own button to close it. There doesn't seem to be any UIKeyboard class or anything like that, I've already gone through UITextField, UIView, UIWindow, UIControl, UIResponder, etc. and I can't find anything.
Thanks, Nate
 

Niiro13

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
Have you worked with delegates before?

Delegates are pretty much "what happens when I do this?"

So the UITextField class has all the "what is it and what does it look like."

The UITextFieldDelegate contains your answer: "textFieldShouldReturn:"

Set the textField's delegate to self. Then implement the delegate by adding it in the header file of the same class.

Now use the "textFieldShouldReturn:" method and use the [textField resignFirstResponder];.

Don't forget to return YES.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
No... I can't find a tutorial or example that I can understand, it's a work in progress. But thanks, this should be enough.
Nate
 

Niiro13

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
No problem. I had no idea what delegates were when starting out (came from Java and never used a delegate there).

Turns out delegates are very, very useful.

You get to control stuff you didn't think you'd be able to, like for scrollviews, you can control when the person started scrolling,when they lift their finger, and even when it starts to deaccelerate. Textfields you even get to control what happens everytime they type a character.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
Could you possibly point me at whatever docs/tutorials you have handy for delegates? I figure if I get enough on the same topic, I'll learn them one way or another :rolleyes:
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
Thank you you're a life saver! However, I still have no idea how to use delegates and google isn't being nice to me, nor is apple's docs. Any help there would be appreciated.
Thanks a million,
Nate
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Essentially, (for the built-in protocols at least) you declare your class to be a delegate of something (list here, under "Protocol References": https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKit_Framework/index.html)

For example, making your class a UIWebViewDelegate from the header:

Code:
@interface WebViewController : UIViewController <UIWebViewDelegate>

Then in your implementation you set the UI component you want the protocol to work with, which in this example could be:

Code:
webView.delegate = self;

(Where webView is a UIWebView.)

Then, when certain things happen within the UIWebView, for instance a page starting or finishing loading, methods in your implementation class will be fired. The list of methods can be found in each delegate's protocol reference (listed above), so for instance UIWebViewDelegate is here: (https://developer.apple.com/iphone/...iewDelegate_Protocol/Reference/Reference.html)

And there you can see:

Code:
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:

The names are fairly self-explanatory, and the docs are pretty good too :)

Proper explanation from the Cocoa docs here: http://developer.apple.com/iphone/l...unicatingWithObjects/chapter_6_section_4.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.