selectall uitextfield does not always select all - iphone

- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField selectAll:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
In the above, the textField selects correctly but when I return from the keyboard and tap the textField for a second time consecutively, it does not select the text. If I do not pick it consecutively or if I deselect the text before returning from the keyboard, the next focus of that textField selects the text correctly.
How can I select the text in the abovementioned case?

I have found a perfect solution(invoke selectAll in next runloop):
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:#selector(selectAll:) withObject:textField afterDelay:0.f];
}

I solved this issue using Grand Central Dispatch. You can wrap [textField selectAll:self]; with a dispatch_async call and dispatch_get_main_queue() as a first parameter.
dispatch_async(dispatch_get_main_queue()){
// ... code you want to run on the main queue goes here
}

Related

UITextfield keyboard resign

in my uitableview in each cell I'm having uitextfield when user edits the textfield and after pressing any other button on the screen the Keyboard is not resigning. I did the following in textfield delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textfieldInCell = textField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
textfieldInCell=nil;// this is the ivar i am using for each textfield in cell.
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
I am also calling the shouldReturn delegate function once the user tapping on any other button but the keyboard is not resigning. Where am I going wrong?
Confirm that you bind the delegate of each textfield that you create with the view controller and add one line of code in textfield did end editing :-
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// add the following line
[textField resignFirstResponder];
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
have you bind delegate of textfield with your controller? or did you check textFieldShouldReturn calling or not?
i think, binding is missing wit view controller in your case.
thanks
add a line in your tableview where you are adding textfield.
<YOUR_TEXTFIELD>.delegate = YES;
Enjoy Programming
first check that you give the delegate or not and give the delegate to UITextField after that when you call the method of button at that time resign this textfield like bellow...
-(IBAction)yourButton_Clicked(id)sender{
[yourTextField resignFirstResponder];
////your code write here
}
you must try to assign tag value to textfield then in should return method you used that tag to hide the keyboard like this (if you assign the tag -1 then)
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag==-1){
[textField resignFirstResponder];
}
}

How can I make the keyboard of other text field disappear while accessing other text field

I have two text fields in my view. I did it using IB. in second text field i am using the action sheet
After entering the text in textField1 I am in text Field-2.In second text field i am using the action sheet with a picker to select the date. so i resigned the textfield -2 keyboard before opening the action sheet. after i dismiss the action sheet when i tried to resign the keyboard it is not returning.
i used
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textfield1 resignFirstResponder];
return YES;
}
to resign the textfield1 ..
- (void) viewDidLoad
{
//don't forget to add <UITextFieldDelegate> in your .h
firstTextField.delegate=self;
secondTextField.delegate=self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==2)
{
//Here you can call function to view your datepicker
return NO; //Will not open keyboard for second textfield.
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
You have return current textField textFieldShouldReturn method change your textFieldShouldReturn metod like below
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
You need to Give the tag for TextField,
Within this method,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 2)
{
[textField1 resignFirstResponder];
// your action sheet code here
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textfield resignFirstResponder]
return YES;
}
Set tag to every text fields and check them in
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if([textfield.tag == 1])
{
[textFieldFirst resignFirstResponder];
}
else
{
// your action sheet code here
}
return YES;
}

How to tell if a UITextField is firstResponder

I'm working on an app that has a signup feature. The signup process is broken up into 3 different views, each of which has 2 UITextField to take user input. I have also implemented the UITextFieldDelegate methods;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
and
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Depending on which UITextField is in focus will determine what method is called when the user taps the return key on the keyboard. I would like to just call my resignFirstResponder method for the first 4 UITextFields but on the last textField I would like to call my join method when return is pressed.
So my question is; How can I determine which UITextField just called the textFieldShouldReturn: method?
As always, thanks in advance!
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theLastTextField) {
//joinmethod
} else {
[textField resignFirstResponder];
}
}

UITextField strange behaviour on resignFirstResponder

Already the second day and cannot figure out the problem,
I've UITabelView with Custom UICellViews, each custom UICellView consists of UILabel and UITextField.
Custom UICellView object allocs UITextField and UILabel in its init method and are released in dealloc.
The number of custom UICellViews in UITableView is 6.
The user scenario is following
When user clicks from from 1 to 5 UITextFields virtual keyboard opens and user types some text
When user clicks on the 6th UITextField if virtual keyboard is active, it should be hidden, and if it is hidden it shall not be displayed.
As implement UITextFieldDelegate protocol in my UIViewController class and set the delegate of each UITextField to self.
My delegate methods are following
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
[textField resignFirstResponder];
return NO;
}
}
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
/* Some code */
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
All the functions are properly !
So now, the virtual keyboard is never get hidden, why this happens ?
PS. Similar code has worked on iPhone but this issue exists on iPad.
You need to know which textfield was last used! so you can do [lastUsedTextField resignFirstResponder]
There is a dirty, but working trick.. you can make your textfield the new active UITextField and call resignFirstResponder in the next cycle immediately:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
// this will schedule keyboard dismissal for the current text field
dispatch_async(dispatch_get_main_queue(), ^{
[textField resignFirstResponder];
});
return YES; // -> make this one active
}
}
did you seted action for textField?
[YourTextField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
PS set any selector for any ControlEvent

keyboard is not getting dismissed on UITextField

This may sound a newbie question, however I'm new to iOS dev.
Platform : iPad
I have a UITableView with UITextField, let say they are two.
When pressing on the first one virtual keyboard should appear, but when user tapps on the second UITextField the virtual keyboard should be hidden and data picker view should be displayed.
So here it is, how I did it.
-(void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag != PICKER_VIEW_TAG) {
...
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[self countriesPickerView];
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
return YES;
}
So now the question, when I click for the first time on the first UITextField it displays keyboard, but when I switch to second one it does not hide it. Why ? and how to solve this ?
UPDATE : The corresponding textField is not getting selected but i.e. the resign takes place, right ? but the keyboard is not hidden ... why this happens ?
The issue is with your textFieldShouldReturn. If you want it to complete the action, allow it to return while resigning the first responder.
[textField resignFirstResponder];
return YES;
Set the inputView property on UITextField to display views other than keyboard to receive input. In this case, you would set the text field's inputView to be an instance of UIDatePicker. The picker will be displayed with the same keyboard animation automatically and you get to delete a bunch of code. Win/win.