objective c textField: selectAll text doesn't always work - ios

I implemented this in the UITextField delegate:
-(void)textFieldDidBeginEditing:(UITextField *)iTextField {
[iTextField selectAll:iTextField];
}
My text field contain text.
When tapping on it, the keyboard goes up and all text selected.
when dismissing the keyboard and tapping again, no text selected (just blinking cursor).
when dismissing the keyboard and tapping again, all text selected again.
Any clue why does no text selected at the second tap?

have you tried with this?
textField.selectedTextRange = [textField textRangeFromPosition:textField.beginningOfDocument toPosition:textField.endOfDocument];
EDIT 1:
Now is going to work :), this call will be at the end of the queue
[textField performSelector:#selector(selectAll:) withObject:nil afterDelay:0.0];

I call selectAll in viewDidAppear works.

Related

How to set focus not on the UITextView when it is shown?

In some cases when the UITextView in shown, I want it does not show focus and soft keybaord, when user clicks on it, both will show.
But in some other cases, I want the focus and soft keybaord shown when the UITextview is shown.
I know how to show the focus but I don't know how to let UItextview lose focus.
Any help is appreciated. thank you!
-(void)viewWillAppear:(BOOL)animated {
if(isEditMode) {
[self.textView becomeFirstResponder];
} else {
[self.textView resignFirstResponder];
}
.....
}
UITextView is a subclass of UIResponder.
To make text view first responder (get focus) use:
textView.becomeFirstResponder()
To make text view resign first responder (loose focus) use:
textView.resignFirstResponder()

Tapping between UITextFields in iOS7

When a UITextField is selected and has a keyboard shown, if I tap other parts of the view the keyboard disappears.
If I tap another UITextField, the keyboard stays up, the first textfield is deselected, and nothing happens. Then I need to tap on the other UITextFIeld again for selection to happen and a keyboard to appear.
Is there a way to make a second UITextField immediately accessible when a first UITextField is selected?
If you reload the tableview in textFieldDidEndEditing, you'll break selection in this way. Don't do that.
try it, press another view should call below fn.
-(void)disappearKey{
[self.view endEditing:YES];
}
after keyboard disappear, Tap any textfield, will appear keyboard.
First of all I think its a bug that the keyboard is not dismissed and opened again when tapping on another UITextField or UITextView. It should be reported and Apple should fix it.
Using the textfield delegate methods and registering for keyboard notification it should be possible to manually keep track if the user tapped on another textfield and the keyboard did not close and reopen. At the very least you should be able to detect when this is happening and close the keyboard manually by [textField resignFirstResponder];
The keyboard notification are as follows:
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
I'm''pretty sure you know the UITextfield and textview delegate methods
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
I am not in an active project at the moment so I'm not sure if I just ignored the problem but I can't recall this happening to me.
you can use BSKeyboardControls. just see the demo and decide to
use or not.
or you can do you have to set tag in sequence to the each textfield
in uiview. then use the below code.
-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

how to make the textfiled clear on click

i have app i want that when user enters any data and text field has by default 10 so when user click on textfield then it clear the textfield to enter data.It mean when user enters data it doest not delete one bye one like clear 1 and o with back space but by default when user click textfield it should get clear.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textfield settext:#""];
}
if you focus on textfield then this method is called and textfield is cleared.
Apple Developers already did this for you, set the 10 into UITextField placeholder property see doc, when you tap on the textfield, it will be remove automatically!
However, if you want to clear it in case you've added 10 as text of textfield then,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = #"";
}
It will call each time when you focus to textfield.

Bringing up the iPhone keyboard by clicking a button?

How can I bring up the iphone keyboard by clicking a button - I haven't created any text fields. My intention is to have labels that hold a single character.
Other people have asked this and it has been answered but it was from years ago and I didn't understand what people meant in there responses.
Thank you for any help in advance.
Only way you could do this is via hidden UITextField, and set that textField to becomeFirstResponder
you could hide the text field in your code liket his textfield.hidden=YES;
or you could hide the textfield from nib file also by going into the attribute inspector and tick the Hidden property
You could have a look at this UIKeyInput- Displaying keyboard in iPhone without UITextField or UITextView, I have not tried myself this
Attach the button to a touchUpInside event and call becomeFirstResponder on the textView or textfield:
Here is an example (to bring it up when a text view is clicked):
-(IBAction)buttonClicked:(id)sender{
[textView becomeFirstResponder];
}
-(void)ButtonClicked
{
[textFld becomeFirstResponder];
[textView becomeFirstResponder];
}
use this method
You can add a hidden UITextFeild as tempTF and when the button clicked. call becomeFirstResponder of this textFeild:
[self.tempTF becomeFirstResponder];
To Hide textField:
UITextField *tempTF = [[UITextField alloc] init];
tempTF.hidden = YES
or mark Hidden ir your Interface Builder

Resources