iPhone connect textfield end on exit event to button press event

I have a text field and a button. When the button is pressed, it calls a routine. I want the textfield end on exit to call the same routine without having to duplicate the code. ViewController.h is below

    #import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
@property (weak, nonatomic) IBOutlet UITextField *search;

- (IBAction)calculate:(id)sender;

@end

Answers


In ViewController.h implement UITextFieldDelegate like so:

@interface ViewController : UIViewController <UITextFieldDelegate>

and then use the method

-(BOOL)textFieldShouldReturn:(UITextField *)textField

and call your IBAction and resignFirstResponder, I would also auto-enable return key.


[EDIT1]

In your viewController header:

@interface ViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
@property (weak, nonatomic) IBOutlet UITextField *search;

-(void)doSomeWork;
-(IBAction)calculate:(id)sender;

@end

Simply implement the functionality in a 2nd tier routine.

In your viewController.m file:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(search == textField){
        [textField resignFirstResponder];
        [self doSomeWork];
    }
    return YES;
}

-(void)doSomeWork{
    // Do whatever you want to do here!!!
}

-(IBAction)calculate:(id)sender{
    [self doSomeWork];
}

In your xib file you will have to connect up the button to the "calculate" action, the search to the proper UITextField.

You can set the delegate of the UITextField either graphically in the Interface Builder, or in code. If in code, then within your viewController.m file add the line:

search.delegate = self;

to your viewDidLoad method as follows:

-(void)viewDidLoad{
    [super viewDidLoad];
    search.delegate = self;
}

Need Your Help

how should I passing byte array buffer to writefile

c++ c windows-ce win32gui

I am trying to write the 16byte data to the serial port, I am having the trouble to convert the value to lpcvoid, here is my code,

Fortran Mex File - Incorrect Values Passed Back to Matlab

matlab fortran mex

I've written a subroutine in Fortran to handle a computationally intensive portion of my code. I want to link it to Matlab using a mex function. Here's the simplest version that gives the relevant ...