Here is image of view
I have following problem.
I created a sample user information form for user. He fills some details and submits. See the first sub image in above screenshot.
I am entering first name and last name here, normal keyboard will appear when I switch first name to last name manually by scrolling a view or by using keyboard next button on keyboard.
Then, when click code textfield, a uipickerview/UIActionsheet will appear. Plase see previous image.
It is working fine. When I move one firstname textfield to last name text field and from lastname to code text field.
Problem : My problem is that, suppose currently I am lastname textfield i.e I am entering my last name and I clicked on code textfield following issue is coming. seen the below image.Here is my problem
So here, keyboard is appearing as well as uipickerview/UIactionsheet is also coming but in behing keyboard. I don't know it is coming.
Not only last name to code textfield form first name tp code textfield or email textfield to code textfield...same issue is coming only when I move to one textfield to next field by scrolling/switching. If I use next button from keyboard it is moving and working file.
Anybody help me to sort out this problem.
Here my code,
#interface CreateTicketViewController ()
- (void)countryCodeWasSelected:(NSNumber *)selectedIndex element:(id)element;
#end
#implementation CreateTicketViewController
- (IBAction)countryCodeClicked:(id)sender {
[self removeKeyboard];
[ActionSheetStringPicker showPickerWithTitle:NSLocalizedString(#"Select CountryCode",nil) rows:_countryArray initialSelection:0 target:self successAction:#selector(countryCodeWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:sender];
}
- (void)actionPickerCancelled:(id)sender {
NSLog(#"Delegate has been informed that ActionSheetPicker was cancelled");
}
- (void)countryCodeWasSelected:(NSNumber *)selectedIndex element:(id)element{
// self.selectedIndex = [selectedIndex intValue];
//may have originated from textField or barButtonItem, use an IBOutlet instead of element
self.codeTextField.text = (_codeArray)[(NSUInteger) [selectedIndex intValue]];
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField==_codeTextField) {
[_codeTextField resignFirstResponder];
_codeTextField.tintColor = [UIColor clearColor];
[self removeKeyboard];
[ActionSheetStringPicker showPickerWithTitle:NSLocalizedString(#"Select CountryCode",nil) rows:_countryArray initialSelection:0 target:self successAction:#selector(countryCodeWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:self.view];
//return NO;
}
Here is method for removeKeyboard.
UIToolbar *toolBar= [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *removeBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(removeKeyBoard)];
-(void)removeKeyboard{
[_emailTextField resignFirstResponder];
// [_mobileTextField resignFirstResponder];
// [_msgTextField resignFirstResponder];
[_subjectTextField resignFirstResponder];
[_firstNameTextField resignFirstResponder];
}
again same question, check my solution..! I already posted answer for this.
https://stackoverflow.com/a/46903445/8174920
You have to do like this :
_codeTextField.inputView = YOURPICKER
Related
I have managed to add a UIPickerView as an input view to UITextField in viewDidLoad, but now I want to make the UITextField as first responder when another textfield (with tag =4) ends editing. This is the code I'm using:
This is in the viewdidload, and all the other code needed to initialize the picker view is there:
UIPickerView *myPickerView = [[UIPickerView alloc] init];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.backgroundColor = [UIColor colorWithRed:76/255.0 green:76/255.0 blue:76/255.0 alpha:1];
textfieldInput.inputView = myPickerView;
This is in my text field did end editing method:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 4){
[textfieldInput becomeFirstResponder];
}
The problem is that when the textfieldInput becomes the first responder, the picker view isn't shown, instead the keyboard is presented.
Also, it should be mentioned that i have another textfield on top of this that gets resigned if it became the first responder and becomes hidden. This textfield is the textfield that has a tag of 4. (This textfield has been added for design reasons).
This code works for me... are you sure that call the correct reference of textfield?
try: [self.textfield becomeFirstResponder];
I'm trying to move my first responder on using tags in a tableview cell. I've set _txtFieldActive to pick up the active UITextFields tag. I can see this when I press the next button on the keyboard via NSLog. Now however I can't seem to figure out how to resignfirstresponder on that tag, and then move my first responder onto tag 102?? I get an error on the line of code trying to assign tag 102 to *tmp.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
switch (textField.tag)
{
case 101:
//Do Nothing do not want to close keyboard but move on to next UITextField
if (_txtFieldActive.tag == 101)
{NSLog(#"Tag = 101");
UITextField *tmp = [textField.tag == 102];
[tmp becomeFirstResponder];
}
break;
case 102:
[textField resignFirstResponder];
}
return YES;
}
Many thanks all for your help in advance for any pointers.
Jon.
To get a reference to a view in the current view's hierarchy with a given tag, we need to call viewWithTag:.
if (_txtFieldActive.tag == 101) {
NSLog(#"Tag = 101");
UITextField *tmp = [self.view viewWithTag:102];
[tmp becomeFirstResponder];
}
Try that on for size.
If this is a UITableViewController subclass rather than a UIViewController subclass, you might need [self.tableView viewWithTag:102];, but self.view should work in either case.
In my app I have a view that is a form that has quite a few inputs.
When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.
If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:
calling resignFirstResponder in textFieldDidEndEditing
calling [self.view endEditing:YES] in textFieldDidEndEditing
calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.
Any ideas would be great.
I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour.
http://dl.dropbox.com/u/61692457/KB_Test.zip
Declare a Global UITextField in .h file
UITextField *txtfld;
Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 || textField.tag==3)
{
if(numPickerPopover == nil)
{
numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];
numPicker.delegate = self;
numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
[numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
}
[numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[txtfld resignFirstResponder];
return NO;
}
if (textField.tag == 2)
{
txtfld = textField;
return YES;
}
return YES;
}
To dismiss the keyboard when the user touches the textField that brought it up, add this method:
- (IBAction)dismissKeyboard:(id)sender {
[textField resignFirstResponder];
}
In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).
In my app I have a pretty large form to be filled out. The form itself is made with a table view and static cells mode. Each of the cells containing a label and a textfield. I wanted to add a toolbar above the keyboard to enable navigating between the text fields. I did that, however it is behaving very strange (at least I think).
I have no problems jumping to the next text field while jumping to the previous textfield is only possible if it is visible. If it is not visible the old textfield stays first responder, however the moment I scroll my table view and the intended text field becomes visible, it becomes the first responder (I do not click into it!). Well this is of course not the behavior that I wanted.
My question is: Is this kind of behavior normal? Could you somehow circumvent it?
If you want to see this kind of behavior yourself I have uploaded an Xcode project that illustrates the problem. You can download the zipped project here: download. The main parts of the code are explained below.
I set up a grouped table view with static cell. Each of them containing a label and a textfield. I created outlets for every textfield to gain access to them. In my viewDidLoad method I create the toolbar and its buttons, store the textfields in an array and set the controller to be the textfields delegate.
- (void)viewDidLoad
{
[super viewDidLoad];
// create the keyboard toolbar with navigation elements
self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(prevClicked:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextClicked:)];
[self.keyboardToolbar setItems:[NSArray arrayWithObjects:prevButton, nextButton, nil]];
// create the field chain
self.fields = [NSArray arrayWithObjects:self.aField, self.bField, self.cField, self.dField, self.eField, self.fField, nil];
// for scrolling and keeping track of currently active field
NSInteger max = [self.fields count];
for(NSInteger i = 0; i < max; i++) {
UITextField *curr = (UITextField *)[self.fields objectAtIndex:i];
curr.delegate = self;
}
}
The text field delegate methods store a reference of the active text field and prevent line breaks from being inserted.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.inputAccessoryView = self.keyboardToolbar;
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// set the current active textfield
self.currentField = textField;
// scroll this textfield to top
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.currentField = nil;
return NO;
}
And finally there are the selectors for the toolbar buttons and the logic to get the next textfield.
- (void)moveResponderByStep:(NSInteger)step
{
// only move if a textfield is the current responder
if(![self.currentField isEditing]) {
return;
}
// where we are and where to move
NSInteger max = [self.fields count];
NSInteger moveToNumber;
for(NSInteger i = 0; i < max; i++) {
if(self.currentField == [self.fields objectAtIndex:i]) {
moveToNumber = i + step;
}
}
// stay in bounds
if(moveToNumber >= max || moveToNumber < 0) {
[self.currentField resignFirstResponder];
return;
}
// move on
UITextField *next = [self.fields objectAtIndex:moveToNumber];
[next becomeFirstResponder];
}
- (void)prevClicked:(id)sender
{
[self moveResponderByStep:-1];
}
- (void)nextClicked:(id)sender
{
[self moveResponderByStep:1];
}
Thank you!
Try checking first to see if the row is visible. Scroll the row to visible if it isn't. Then set the text field as first responder.
I created a form and the keypad (Numeric only) appears when entering data like your age.
I want the keyboard to disappear when the user taps the background and I want to add a "Done" button in the empty slot under the 7 (next to the zero). (im using the Number Pad keyboard)
I found this example but I have a few questions.
In
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
If I have more than 1 text field in my form.
Will I need to write every textfield in the dismissKeyboard method?
Easy way to do this is to use the method provided in UIView
- (BOOL) endEditing:(BOOL)force;
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
So just do this:
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
and it will support any more text fields you add on your page (under that UIView of course)
You should only send dismissKeyboard to that textField that you are currently editing.
In your code you have got memory leak. Better use this one:
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
To check if UITextField is currently in edit mode you can check its property:
A Boolean value indicating whether the text field is currently in edit mode. (read-only)
#property(nonatomic, readonly, getter=isEditing) BOOL editing
For example, you have 3 text fields then dismissKeyboard will look something like this:
-(void)dismissKeyboard
{
UITextField *activeTextField = nil;
if ([textField1 isEditing]) activeTextField = textField1;
else if ([textField2 isEditing]) activeTextField = textField2;
else if ([textField3 isEditing]) activeTextField = textField3;
if (activeTextField) [activeTextField resignFirstResponder];
}
I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a UIControl, rather than a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions to be performed when interacting with the view.
Here's how to do it:
In Interface Builder, select your view. Then, assign its class to UIControl. (It's probably set up as UIView currently.
In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:
- (IBAction)backgroundTap:(id)sender
{
if ([textField1 isEditing]) {
[textField1 resignFirstResponder];
} else if ([textField2 isEditing]) {
[textField2 resignFirstResponder];
}
}
Finally, in Interface Builder, connect the IBAction you created to the UIControl.
Read this article, it may help you
Writing iOS 4 Code to Hide the iPhone Keyboard (Xcode 4)
Here i give common text field object. and asign reference to it in "textFieldShouldBeginEditing" method. that will common for all text field..
In this case, you need to dismiss one text field that will hide keyboard..
declare textfield object in .h file.
#interface RootViewController : UIViewController
{
IBOutlet UITextField* txt_common;
}
IN .m file
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
-(void)dismissKeyboard
{
NSLog(#"hi");
[txt_common resignFirstResponder];
}
#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"hi++++++++++++++++++");
txt_common=textField;
return YES;
}