iPhone connect textfield end on exit event to button press event - iphone

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

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;
}

Related

Focus on UITextField does not work

In my iPhone application I have a UIScrollView with several UITextFields.
Using BSKeyboardControls I have added Prev/Next/Done buttons to move between the fields. However, the focus on the selected field is not working, meaning that the text field is actually still under the keyboard although selected.
becomeFirstResponder is activated but just don't set the focus.
Any ideas what might be wrong?
Thanks
In the H file
#import "BSKeyboardControls.h"
...
#interface AddClientViewController : BaseViewController<UIAlertViewDelegate, UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate>
...
#property (strong, nonatomic) IBOutlet UITextField *firstName;
#property (strong, nonatomic) IBOutlet UITextField *lastName;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *mobile;
#property (strong, nonatomic) IBOutlet UITextField *birthday;
#property (strong, nonatomic) IBOutlet UITextField *anniversary;
#property (strong, nonatomic) IBOutlet UITextField *street;
#property (strong, nonatomic) IBOutlet UITextField *city;
#property (strong, nonatomic) IBOutlet UITextField *state;
#property (strong, nonatomic) IBOutlet UITextField *zip;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) BSKeyboardControls *keyboardControls;
....
In M file
- (void)viewDidLoad
{
...
NSArray *fields = #[ self.firstName, self.lastName,
self.email, self.mobile,
self.birthday, self.anniversary,
self.street, self.city, self.state, self.zip];
[self setKeyboardControls:[[BSKeyboardControls alloc] initWithFields:fields]];
[self.keyboardControls setDelegate:self];
}
- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls
{
[keyboardControls.activeField resignFirstResponder];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls directionPressed:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls selectedField:(UIView *)field inDirection:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.keyboardControls setActiveField:textField];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self.keyboardControls setActiveField:textView];
}
The setActiveField in BSKeyboardControls
- (void)setActiveField:(id)activeField
{
if (activeField != _activeField)
{
if ([self.fields containsObject:activeField])
{
_activeField = activeField;
if (![activeField isFirstResponder])
{
[activeField becomeFirstResponder];
}
[self updateSegmentedControlEnabledStates];
}
}
}
Since you're using a UIScrollView with UITextFields, you can use the scrollRectToVisible method for the UIScrollview, in a method that'd be roughly like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[_myScrollView scrollRectToVisible:textField.frame animated:YES];
}
To do this, you'll need to make sure that the UIViewController is the UITextFieldDelegate of each of the textfields in the scrollview. You could do this in the viewDidLoad method of your UIViewController:
[textField1 setDelegate:self];
[textField2 setDelegate:self];
...and so on

Get values from several UITextFields

I have a grouped static UITableViewController, with 3 rows per section. Each row contains a UITextField. How do I get the value for each UITextField?
So far, what I've seen is how to get the value of one UITextField. What should I do if I have to get more than one.
I plan to put the values of these fields in a NSMutableDictionary.
Here's my code for making the table view: (irrelevant parts are omitted)
#import "AddItemTableViewController.h"
#import "iMonggoFetcher.h"
#interface AddItemTableViewController() <UITextFieldDelegate>
#property (weak, nonatomic) NSMutableDictionary *productItem;
//required
#property (weak, nonatomic) IBOutlet UITextField *stockNoTextField;
#property (weak, nonatomic) IBOutlet UITextField *nameTextField;
#property (weak, nonatomic) IBOutlet UITextField *retailPriceTextField;
//basic info
#property (weak, nonatomic) IBOutlet UITextField *costTextField;
#property (weak, nonatomic) IBOutlet UITextField *descriptionTextField;
#property (weak, nonatomic) IBOutlet UITextField *barcodesTextField;
#property (weak, nonatomic) IBOutlet UITextField *tagsTextField;
#property (weak, nonatomic) IBOutlet UISwitch *exemptFromTaxSwitch;
//advanced features
#property (weak, nonatomic) IBOutlet UISwitch *allowDecimalQuantitiesSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *enableOpenPriceSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *disableDiscountSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *disableInventorySwitch;
#end
#implementation AddItemTableViewController
#synthesize productItem = _productItem;
#synthesize stockNoTextField = _stockNoTextField;
#synthesize nameTextField = _nameTextField;
#synthesize retailPriceTextField = _retailPriceTextField;
#synthesize costTextField = _costTextField;
#synthesize descriptionTextField = _descriptionTextField;
#synthesize barcodesTextField = _barcodesTextField;
#synthesize tagsTextField = _tagsTextField;
#synthesize exemptFromTaxSwitch = _exemptFromTaxSwitch;
#synthesize allowDecimalQuantitiesSwitch = _allowDecimalQuantitiesSwitch;
#synthesize enableOpenPriceSwitch = _enableOpenPriceSwitch;
#synthesize disableDiscountSwitch = _disableDiscountSwitch;
#synthesize disableInventorySwitch = _disableInventorySwitch;
- (IBAction)save:(UIBarButtonItem *)sender {
[self.productItem setValue:self.stockNoTextField.text forKey:IMONGGO_PRODUCT_STOCK_NO];
[self.productItem setValue:self.nameTextField.text forKey:IMONGGO_PRODUCT_NAME];
[self.productItem setValue:self.retailPriceTextField.text forKey:IMONGGO_PRODUCT_RETAIL_PRICE];
}
#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// optionally trigger delegate method here
}
#pragma mark - View lifecycle
- (void) viewWillAppear:(BOOL)animated{
//[self.stockNoTextField becomeFirstResponder];
}
- (void) viewDidLoad{
[super viewDidLoad];
self.stockNoTextField.delegate = self;
self.nameTextField.delegate = self;
self.retailPriceTextField.delegate = self;
//will do the rest later, just trying it out for now
}
#end
1) You can make properties of these fields.. and then get the text by the property name.text
2) You can try giving the fields a tag and then get these fields using viewwithtag method. and then their text.
you can take values of each textFields in a array by textField.text.

type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)

// ViewController.h
#interface ViewController : UIViewController{
IBOutlet UIButton *btnname;
IBOutlet UITextView *txtname;
IBOutlet UILabel *lblname;
}
#property (retain, nonatomic) IBOutlet UITextField *txtname;
#property (retain, nonatomic) IBOutlet UILabel *lblname;
- (IBAction)Btntikla:(id)sender;
#end
//ViewController.m
#implementation ViewController
#synthesize txtname;//here it says -type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)- this mistakes
#synthesize lblname;
- (void)dealloc {
[lblname release];
[txtname release];
[super dealloc];
}
- (IBAction)Btntikla:(id)sender {
NSString *name=[txtname text];
lblname.text=name;
}
#end
//my whole program is this.it gives this error?type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)..Can you help me pls?? what is my mistake ?
That's beacause you have
#property (retain, nonatomic) IBOutlet UITextField *txtname;
and
IBOutlet UITextView *txtname;
And UITextField and UITextView are differents controls

iPhone Simulator Text Box Takes up Whole Screen

Here is my code. Not sure what's awry.
controller.h
#import <UIKit/UIKit.h>
#interface ch4iOSPracticeViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
}
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#property (nonatomic, retain) IBOutlet UITextField *numberField;
#end
controller.m
#import "ch4iOSPracticeViewController.h"
#implementation ch4iOSPracticeViewController
#synthesize nameField;
#synthesize numberField;
delegate.m Everything properly released.
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
When you add a textField to the iPhone using interface builder, I am guessing that there was another view already present there; So when you pulled the textField over it, it had stretched. Try pulling it inside a plain View.
It is either that you pulled a textview instead of a textfield.

switching views in iphone [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
switching views in iphone
I am making a client server program in which i want to switch from one view to another but i am getting an error in "clientserverprogram view.m" plz help.i also asked same question five hours ago but the answer was not satisfactory again same error is pertaining.
"clientserverprogram view.h"
#import <UIKit/UIKit.h>
#class secondview;
#interface clientserverprogramViewController : UIViewController {
IBOutlet UITextField *name;
IBOutlet UITextView *filepath;
IBOutlet UIButton *print;
IBOutlet UIButton *settings;
IBOutlet UIButton *cancel;
IBOutlet UILabel *display;
IBOutlet secondview *secondview;
}
-(IBAction) print;
-(IBAction) settings;
-(IBAction) cancel;
#property (nonatomic , retain) IBOutlet UITextField *name;
#property (nonatomic , retain) IBOutlet UITextView *filepath;
#property (nonatomic , retain) IBOutlet UILabel *display;
#end
"clientserverprogram view.m"
#import "clientserverprogramViewController.h"
#import "secondview.h"
#implementation clientserverprogramViewController
#synthesize name ,filepath,display ;
-(IBAction) print {
NSString *str = name.text;
[display setText : str];
}
-(IBAction) settings {
[self presentModalViewController: secondview animated: YES ];
"" error: expected expression before 'secondview'""
}
-(IBAction) cancel {
exit(0);
}
- (void)dealloc {
[super dealloc];
}
#end
"secondview.h"
#import <UIKit/UIKit.h>
#interface secondview : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *back;
}
-(IBAction) back;
#end
""secondview.m""
#import "secondview.h"
#implementation secondview
-(IBAction) back {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void)dealloc {
[super dealloc];
}
#end
I used your code and the problem here is your class name "secondview" and the instance you are making from it IBOutlet secondview *secondview are same. Please use different name for class names and the instances you create.
Always start with uppercase for class names and start with lowercase for class instances you create.
Hence, your class name should be SecondView and you should write IBOutlet SecondView *secondView.
Or you should just use different names. Its very confusing.