Closing the keyboard

Discussion in 'iOS Programming' started by liptonlover, Oct 30, 2008.

  1. macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    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
     
  2. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #2
    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.
     
  3. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    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
     
  4. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #4
    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.
     
  5. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    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:
     
  6. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  7. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #7
    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
     
  8. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #8
    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
     
  9. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008

Share This Page