how to make the textfiled clear on click - ipad

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.

Related

objective c textField: selectAll text doesn't always work

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.

how to check if focus switches from one text view to another

I've got 2 UITextFields. I can detect when the user in inside either of them with:
textFieldDidBeginEditing:(UITextField *)textField
Which works great. But i want the textfields to animate out when the user is not inside either of them. Currently I'm calling: (void)textFieldDidEndEditing:(UITextField *)textField
However, this is called even if i switch from on textfield to the other. Is there a better way to call do this?
What I would do is add a BOOL flag to detect if you should animate out your text fields or not. It would work something like this:
-(void)hideTextFields {
if (self.shouldHideTextFields) {
self.textField1.hidden = YES; // Or whatever you want to do with
self.textField2.hidden = YES; // your text fields
}
}
Declare a method that checks the BOOL flag and decides whether to hide or not the text fields
Whenever either of the textFieldDidBeginEditing:(UITextField *)textField methods are called set the BOOL flag (you can call it 'shouldHideTextFields') to NO.
Whenever either of the textFieldDidEndEditing:(UITextField *)textField methods are called set the BOOL flag to 'YES'. Also, call [self performSelector:#selector(hideTextFields) withObject:nil afterDelay:1]; to give the user a little time to select the other text field. If he/she does, the flag will be set to NO thanks to the above step.
Hope this helps!
On didend check if either of your textviews is currently the first responder with [textfield isFirstResponder] or [textfield isEditing]. There may be a slight delay when one ends and the other takes control. If that's the case then you could do this check after a slight delay using performSelector:afterDelay.
On textFieldDidEndEditing delegate method, do not just perform your disappearing animations, but do that in dispatch_async, checking if there is no textField editing right now.
In case if user just ended editing of one text field, there will be no editing textfield. But if user had switched to another textfield, it will already start editing and it can be easily checked by isFirstResponder method.

iOS - Shifting from UITextField to another doesn't start the Second One

Here is the UI:
I have a table view that has rows including text fields. For example row1 has textField1 and row2 has textField2.
Now, if the user taps the textField1, everything works fine. The delegate is called and everything.
If the user, then, taps the textField2, only the textFieldDidEndEditing: is called for textField1. textFieldDidBeginEditing: is not called for textField2 (I am pretty sure that textField2 delegate is set, because when I then tap textField2 (again), it starts editing (textFieldDidBeginEditing: is now called)
You may think this is not a real problem. The problem is that the iOS keeps showing the keyboard! With no text fields associated to it! I even loop to all text fields in all visible rows and resign them with no luck.
Seems a bug in iOS, right?
Edit: Here is the code in the delegate
- (void) textFieldDidBeginEditing:(UITextField *)textField {
[self fixTableViewOffsets];
RSMCellEditPricesCell *cell = (RSMCellEditPricesCell *) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
self.currentPriceTextField = nil;
[self fixTableViewOffsets];
[self.tableView reloadData];
}
First of all, be careful with the naming conventions, if you are adding a label at the end of a variable that variable should be a UILabel not a UITextField. Second your problem might be in the textFieldDidEndEditing method, you are calling resignFirstResponder on all your text fields(if the cell.priceLabel is a text field). You shouldn't callresignFirstResponder in textFieldDidEndEditing or textFieldSouldReturn because if those methods are called the text fields are already resign as first responder.
I couldn't find a fix for it. However, as a workaround, I disabled the second text field. When the user taps this field, it will trigger the touch event on cell, not the text field which will dismiss the keyboard. Then, I enable all text fields and the user can select this second text field again.
It's not quite a solution and it doesn't provide the best UX. Still, it work.

iOS 6 - Responding to Keyboard Done Button State

In the iOS Apprentice Series eBooks, you are supposed to build a Checklist app. The tutorials have you go through and make a Done button on a button bar that is disabled and enabled based on the text inside of a UITextField object.
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
Now, the keyboard is set to disable the Done button (on the keyboard) if the text field is empty. Is there a way to observe the state of the keyboard done button and have the done button bar button reflect it accordingly?
IE, when the keyboard done button is enabled, the button bar done button is enabled.
UITextField supports what you want through the enablesReturnKeyAutomatically property. This property is from the UITextInputTraits protocol.
When you create your UITextField, set this property.
self.textField.enablesReturnKeyAutomatically = YES;
This means the Return key (whatever it is labeled) will automatically becomes disabled if the text field is empty. And it automatically becomes enabled when text is entered.
There is no way to observe the state of this so you must implement the code you already have for textField:shouldChangeCharactersInRange:replacementString: to update your other Done button.
You may add a target on your text field with the following events.
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllEditingEvents
Example:
In viewDidLoad
[_textField addTarget:self action:#selector(textFieldEditing:) forControlEvents:UIControlEventAllEditingEvents];
Action Method:
- (void)textFieldEditing:(id)sender
{
_doneButton.enable = ([[sender text] length]>0);
}

What does the return value mean for the UI TextField Delegate method textFieldShouldReturn:?

The apple docs offer:
Asks the delegate if the text field should process the pressing of the
return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters textField The text field whose return button was pressed.
Return Value YES if the text field should implement its default
behavior for the return button; otherwise, NO.
Discussion The text field callsthis method whenever the user tapsthe
return button. You can use this method to implement any custom
behavior when the button is tapped.
My question is what does the return value do? I have been implementing the behavior in this method so it makes no difference what is returned. Is this not the correct method to perform the action?
For instance, if I implement a search function, should I trigger the search action in this method or somewhere else.
This is the correct method to trigger an action when the user taps the Return keyboard key (whatever it happens to be labeled).
The return value from the textFieldShouldReturn: delegate method almost never matters. If you are dealing with a single text field then it definitely doesn't matter.
I ran into one issue a while back that made me realize that just under the right situation, the return value does matter. I had a screen with several text fields and then a text view. I was using this text field delegate method to change the first responder from text field to text field to text view. I found that if I returned YES in this delegate method and then made the text view the first responder, the newline was being sent to the text view.
As a result of this, I now always return NO from this delegate method to be safe.
When you press the return button on the keyboard the textFieldShouldReturn is called.
I never experienced any difference between the return value.
Customization example:
If you have two textFields when the user presses return button from first textField, you can give focus to second field in the following way:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn:");
if (textField.tag == 1)
{
UITextField *passwordTextField = (UITextField *)[self.view viewWithTag:2];
[passwordTextField becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
}
return YES;
}
So you can use this delegate method for triggering search functionality.

Resources