Focus on UITextField does not work - iphone

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

Related

Display UIPickerView when UITextField is clicked

I have a screen with many text fields on it. For one particular text field, I need it to show a picker from which the user can pick state names, once the user has picked a state by clicking on the states name, the picker should disappear.
I am not quite sure how to do this, I have made the Nib file which looks like this.
And here is my header file:
#import <UIKit/UIKit.h>
#import "APIHelper.h"
#interface AddNewAddressViewController : UIViewController <UIPickerViewDelegate,
UIPickerViewDataSource, UITextFieldDelegate>
{
APIHelper *myAPIHelper;
NSMutableData *responseData;
NSURLConnection *theConnection;
UIPickerView *picker;
NSArray *stateNames;
#public IBOutlet UIImageView *imageNewAddressBox;
#public IBOutlet UIImageView *imageNewAddressBox1;
#public IBOutlet UIImageView *imageNewAddressBox2;
#public IBOutlet UIImageView *imageNewAddressBox3;
#public IBOutlet UILabel *labelNewAddress;
#public IBOutlet UILabel *labelStreetAddress;
#public IBOutlet UILabel *labelStreetAddressTwo;
#public IBOutlet UILabel *labelZipCode;
#public IBOutlet UILabel *labelState;
#public IBOutlet UILabel *labelCity;
#public IBOutlet UILabel *labelPhoneNumber;
#public IBOutlet UITextField *textFieldFullName;
#public IBOutlet UITextField *textFieldStreetAddress;
#public IBOutlet UITextField *textFieldPhoneNumber;
#public IBOutlet UITextField *textFieldStreetAddressTwo;
#public IBOutlet UITextField *textFieldCity;
#public IBOutlet UITextField *textFieldState;
#public IBOutlet UITextField *textFieldZipCode;
#public IBOutlet UIButton *buttonNext;
}
#property (nonatomic, strong) IBOutlet UIPickerView *picker;
#property (nonatomic, strong) NSArray *stateNames;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox1;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox2;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox3;
#property (nonatomic, strong) IBOutlet UILabel *labelNewAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UILabel *labelZipCode;
#property (nonatomic, strong) IBOutlet UILabel *labelState;
#property (nonatomic, strong) IBOutlet UILabel *labelCity;
#property (nonatomic, strong) IBOutlet UILabel *labelPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldFullName;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddress;
#property (nonatomic, strong) IBOutlet UITextField *textFieldPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UITextField *textFieldCity;
#property (nonatomic, strong) IBOutlet UITextField *textFieldState;
#property (nonatomic, strong) IBOutlet UITextField *textFieldZipCode;
#property (nonatomic, strong) IBOutlet UIButton *buttonNext;
-(IBAction)addressTextFieldShouldReturn:(id)sender;
-(IBAction)nextPressed;
#end
And here is my .M file:
#import "AddNewAddressViewController.h"
#implementation AddNewAddressViewController
#synthesize imageNewAddressBox;
#synthesize imageNewAddressBox1;
#synthesize imageNewAddressBox2;
#synthesize imageNewAddressBox3;
#synthesize labelNewAddress;
#synthesize labelStreetAddress;
#synthesize labelStreetAddressTwo;
#synthesize labelZipCode;
#synthesize labelState;
#synthesize labelCity;
#synthesize labelPhoneNumber;
#synthesize textFieldFullName;
#synthesize textFieldStreetAddress;
#synthesize textFieldPhoneNumber;
#synthesize textFieldStreetAddressTwo;
#synthesize textFieldCity;
#synthesize textFieldState;
#synthesize textFieldZipCode;
#synthesize buttonNext;
#synthesize stateNames;
#synthesize picker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.stateNames = [[NSArray alloc] initWithObjects:#"Alabama", #"Alaska", #"Arizona", #"Arkansas", #"California", #"Colorado", #"Connecticut", #"Delaware", #"District of Columbia", #"Florida", #"Georgia", #"Guam", #"Hawaii", #"Idaho", #"Illinois", #"Indiana", #"Iowa", #"Kansas", #"Kentucky", #"Louisiana", #"Maine", #"Maryland", #"Massachusetts", #"Michigan", #"Minnesota", #"Mississippi", #"Missouri", #"Montana", #"Nebraska", #"Nevada", #"New Hampshire", #"New Jersey", #"New Mexico", #"New York", #"North Carolina", #"North Dakota", #"Ohio", #"Oklahoma", #"Oregon", #"Pennsylvania", #"Puerto Rico", #"Rhode Island", #"South Carolina", #"South Dakota", #"Tennessee", #"Texas", #"Utah", #"Vermont", #"Virginia", #"Virgin Islands", #"Washington", #"West Virginia", #"Wisconsin", #"Wyoming", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// clicking outside the textfields removes keyboard
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(addressTextFieldShouldReturn:)];
[self.view addGestureRecognizer:tap];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setTitle: #"Update Address Book"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
-(IBAction)addressTextFieldShouldReturn:(id)sender
{
[textFieldFullName resignFirstResponder];
[textFieldStreetAddress resignFirstResponder];
[textFieldStreetAddressTwo resignFirstResponder];
[textFieldPhoneNumber resignFirstResponder];
[textFieldState resignFirstResponder];
[textFieldCity resignFirstResponder];
[textFieldZipCode resignFirstResponder];
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [stateNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [stateNames objectAtIndex:row];
}
#end
So, all I want it do is, when the user clicks on the textFieldState, a picker with all of the states name in it pops up from the bottom and then the user clicks on the states name and that name appears in textFieldState.
Any help would be greatly appreciated. Thanks.
you can use TextFielđelegate , is worked
#pragma mark- text delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField==txtMonth) {
[txtMonth resignFirstResponder];
if (monthClick==0) {
monthPicker.hidden=NO;
monthPicker = [[AFPickerView alloc] initWithFrame:CGRectMake(98.0, 123.0, 126.0, 197.0)];
monthPicker.dataSource = self;
monthPicker.delegate = self;
[monthPicker reloadData];
if ([txtMonth.text isEqualToString:#""]) {
txtMonth.text=[NSString stringWithFormat:#"%i",1];
}
[self.view addSubview:monthPicker];
monthClick=1;
}
}
}
I think you need to implement delegate methods for UITextField, when the
- (void)textFieldDidBeginEditing:(UITextField *)textField
method is called you set the frame of uipicker and show it.

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

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

Hmm, getting : may not respond to '-presentModalViewController:animated:'

I`m getting this annoying warning :
warning: 'AppDelegate' may not respond to '-presentModalViewController:animated:'
In this implementation file :
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
at the line :
self presentModalViewController:controller animated:YES];
and here is the header file :
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
UIImageView *infoButton;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) IBOutlet UIImageView *infoButton;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
Obviously there is something I`m not getting, but I would appreciate if someone could help :)
You must persent modal view controllers from an instance of UIViewController (not your application delegate). If you check out the documentation on UIViewController, you can access a sample application using the modal presentation.