if UITextField clicked - ios

I am doing an application and I need to detect when a UITextField is clicked. I tried using touchesBegan but it doesn't react to textfield when clicked, only outside of it. I am only starting with Objective-C so if you give me advice, please let it be detailed. Thank you.

You can try the delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// Do something
}
As long as the text field has interaction enabled and is editable. However, a second tap will not be detected if the text field is already the first responder.

Set the textview's delegate property to an object that overloads the textViewShouldBeginEditing: function. You could also use the textFieldDidBeginEditing: function of the same delegate.

Related

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.

picker wheel that only appears when selected

I have a view controller that displays various UITextfields to edit information. For one UITextfield I need a picker wheel to select predefined statuses.
Problem is I don't have enough space to integrate a picker wheel, is there a possibility to make it appear only when the text box is selected?
You could set the UIPickerView as the UITextField's inputView.
This will ensure that the picker is shown automatically when the text field gets focus, and hides it when lost.
E.g.
myTextField.inputView = self.myPickerView;
See the documentation on this property.
Assuming your "picker wheel" is a UIView, just hook up your controller as the UITextField's delegate and implement the following:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.pickerWheel.hidden = NO;
}
You need to call your UIPicker in (BOOL)textFieldShouldBeginEditing:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
//call your wheel in here
}
}
Look at
How to Show UIPickerView when selecting UITextField

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.

How To Stop keyBoard Appearence at click event on UITextview?

In my IPad Application i am using TextView only for Text Displaying.As i need to display a Larger Text Thats Why i am using UITextview due to its Scrolling Property instead of using UILabel.
In my application i do not need to edit Text in UITextview ,but problem for me is that when i click on Textview for scrolling the keyboard appear its hide my textview so i want that my keyboard is never appear on click event.i make a search but not find any Suitable solution.Any help will be appriated.Thanx
NEW ANSWER (previous one was not working properly)
OK so since that is not working because it disables scrolling also, you should try to:
Implement UITextFieldDelegate protocol
In your view controller add the text
#interface YourViewController () <UITextViewDelegate>
In viewDidLoad set yourself as a delegate:
yourUITextView.delegate = self;
Implement the delegate method below:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return NO;
}
When the textview is about to edit the text, this method will be called automatically. It returns no, so the editing won't start.
It is very important that you undo the changes from the previous answers: Do not set the editable field to NO
I tried it and it's working. Hope it helps!
OLD ANSWER
when you declare the variable, or in your viewdidload method, set the editable property to NO:
yourUITextView.editable = NO;
or
[yourUITextView setEditable:NO]
That should prevent the keyboard from appearing.
Go to .XIB file and you can uncheck behavior editable or programmatically
textView.editable = NO;

CustomTextField - AutoComplete/AutoCorrect won't dismiss on tap

So I created a custom text view using core graphics and have it conformed to the UITextInput and UITextInputTraits protocols. Everything works fine except for one weird/annoying behavior. The keyboard correctly displays auto correct suggestions, but when the use taps on a suggestion labelled with an 'X', it doesn't dismiss the suggestion but instead inserts the suggestion. I've checked, and in all other programs tapping on the a suggestion with an 'X' dismisses the suggestion. How do I fix this?
In my custom text view I have the following iVars:
//UITextInputTraits
UITextAutocapitalizationType _uiAutoCap;
UITextAutocorrectionType _uiAutoCorrect;
UITextSpellCheckingType _uiSpellCheck;
UIKeyboardType _uiKeyboard;
UIKeyboardAppearance _uiKeyboardAppearance;
UIReturnKeyType _uiReturnType;
BOOL _uiEnableAutoReturn;
BOOL _uiSecureText;
Which are synthesized to the appropriate TextInputTraits properties:
#synthesize autocapitalizationType=_uiAutoCap, autocorrectionType=_uiAutoCorrect, spellCheckingType=_uiSpellCheck, keyboardType=_uiKeyboard, keyboardAppearance=_uiKeyboardAppearance, returnKeyType=_uiReturnType, inputDelegate=_uiTextDelegate, enablesReturnKeyAutomatically=_uiEnableAutoReturn, secureTextEntry=_uiSecureText;
And they're initialized with the following default values:
_uiAutoCorrect = UITextAutocorrectionTypeDefault;
_uiSpellCheck = UITextSpellCheckingTypeDefault;
_uiKeyboardAppearance = UIKeyboardAppearanceDefault;
_uiAutoCap = UITextAutocapitalizationTypeNone;
_uiReturnType = UIReturnKeyDefault;
_uiEnableAutoReturn = NO;
_uiSecureText = NO;
_uiKeyboard = UIKeyboardTypeDefault;
Any ideas?
Edit: Possible answer
When you tap to close the suggestion, your tap is likely intercepted by your view first which presumably changes the selected range of the text (which causes UITextInput to accept the suggestion). Not the best solution, but UITextInput calls
- (NSDictionary *)textStylingAtPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction;
when it wants to make a suggestion, so, you could have an ivar (BOOL) that stores whether or not there is a suggestion (make its value NO whenever a UIKeyInput method is called and YES when the textStyling method is called). Then, modify your gesture recognizer so that it will not change the selection if the aforementioned ivar is YES and the tap is in the rect of the suggestion box (You could get this rect by doubling the height of the rect returned from - (CGRect)firstRectForRange:(UITextRange *)range;). Hope that works.
Edit: you should just be able to implement the UIGestureRecognizerDelegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
So that it only receives a touch if touch.view == (yourTextView)
I am having the same issue, and don't yet have a solution; however, I do believe that you should conform to UITextInputTraits by creating functions that return the value you would like for the property. Example: for the trait UITextAutoCorrectionType to have a value of UITextAutocorrectionTypeDefault, you should provide an accessor method:
- (UITextAutocorrectionType)autocorrectionType {
return UITextAutocorrectionTypeDefault;
}

Resources