How to handle keyboard events

Discussion in 'iOS Programming' started by drf1229, Jun 23, 2009.

  1. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #1
    I am making a program that requires the user to submit some input. I have a submit button, three text fields, and an IBAction. How do I make the action run when the user presses enter on the keyboard? Any help will do, I'm a beginner at iphone development.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    So you want to user to be able to use the Submit button or enter on the keyboard to perform the same action?
     
  3. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #3
    Yes, but I have no idea how to do that. I also want the keyboard to hide when the action starts. (I'm still working on the same project as the last post btw)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I believe you'll need to do these things programatically (vs. thru IB). Look into using resignFirstResponder to hide the keyboard. The same place you do that, you probably can also call your IBAction method.
     
  5. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  6. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #6
    Actually, I'm still a bit confused...
    I did this:

    Code:
    [C resignFirstResponder]
    But that does nothing... What exactly does resignFirstResponder do?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Here's an opportunity to learn from documentation as well as what others have discovered. I.E. the answer is out there...
     
  8. isharan macrumors member

    Joined:
    Jun 3, 2009
    #8
    resignFirstResponder resigns the first responder status of the text field, making the keyboard disappear.
     
  9. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Where did you put your resignFirstResponder? That's key.
     
  11. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #11
    Code:
    //
    //  ViewController.m
    //  PercentFinder
    //
    //  Created by Danny Flax on 6/21/09.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <stdio.h>
    #import "PercentFinderAppDelegate.h"
    @implementation MyViewController
    @synthesize C;
    
    
    
    
    
    
    
    -(IBAction)Solve:(UIButton*)sender{
    	
    	[C resignFirstResponder];
    	
    		NSString*as=[A text];
    	float a=[as intValue];
    	NSString*bs=[B text];
    	float b=[bs intValue];
    	NSString*cs=[C text];
    	float c=[cs intValue];
    	float d=((-1*b)+sqrt((b*b)-4*(a*c)))/(2*a);
    	float e=((-1*b)-sqrt((b*b)-4*(a*c)))/(2*a);
    	printf("%f and %f\n",d,e);
    	 
    	
    	[Answer setText:[NSString stringWithFormat:@"%f and %f",d,e]];
    		
    		
    
    	
    	
    	
    }
    
    
    @end
    
     
  12. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #12
    Guess what, I googled, and I figured it out! I now know how to execute events when enter is pressed on the keyboard. Thank you for your help still! I have one more question though. How do I shift focus from one textfield to another? In other words, when somebody presses enter in the A textfield, I want the focus to go to B, and so on.
     
  13. isharan macrumors member

    Joined:
    Jun 3, 2009
    #13
    Make the next textfield become the firstResponder. Anytime a textfield is firstResponder, the keyboard will appear.
     
  14. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  15. isharan macrumors member

    Joined:
    Jun 3, 2009
    #15
    Should be in the documentation...somewhere along the lines of makeFirstResponder
     
  16. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    UIResponder. (Which is the superclass of UIView which is superclass to UIControl which is superclass to UITextField.)
     

Share This Page