I do a new post because I have a problem that I can't find the respons on the internet. I'm working with a bluetooth barcoder with a clean view without any viewable textfields. To catch the information of the barcoder I use a hidden textfield and works fine. The problem is when the Barcoder disconnects it appears the keyboard because a field is the first responder. I don't want to resign that field but I don't want the keyboard appears.
I have a function that catch when the keyboard will appear:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector (keyboardWillShow:)
name: UIKeyboardWillShowNotification object:nil];
What I want is to stop the event of the keyboard or if not's possible hide it when appears. Any ideas about it? Any help will very usefull...
Thanks guys!
Dhilip's answer may work for you. If it doesn't here are some alternatives:
1) set the textField.enabled property to NO.
2) Subclass UITextField and return nil for the inputView:
#interface MyTextField: UITextField
#end
#implementation MyTextField
- (void)inputView
{
return nil;
}
#end
If you use your custom textfield class instead of a regular UITextField, it works the same except that you've said to use nil for it's keyboard instead of UIKeyboardView (which is the default).
I cannot understand your question properly, but still i got a suggestion for you.
If you are setting the text in UITextField programmatically, you can set userInteractionEnabled property to No.
Related
Because the keyboard hides the textfields when coming up I implemented a solution for this. Now I also have built in the possibility that the user taps on the next button of the keyboard and directly jumps to the next textfield (tabbing between fields).
The problem now is that the view doesn't scroll if I use becomeFirstResponder only. The keyboard is up but the UIKeyboardDidShowNotification is not triggered and so the view doesn't slide up.
Can I fake such a notification so that the UIKeyboardDidShowNotification is sent by the notification center?
Another option is to use two great libraries for this. BSKeyboardControls for the tabbing and TPKeyboardAvoiding for moving the view.
If you just want to send the notification my guess is that [[NSNotificationCenter defaultCenter] postNotificationName: UIKeyboardDidShowNotification object:self] should work just fine.
The solution is simple:
resignFirstResponder
becomeFirstResponder
I can only provide the solution for C# (that's why I left it out first):
private bool TextFieldShouldReturn (UITextField textfield)
{
if (textfield.Tag == 1) {
UIResponder nextResponder = this.View.ViewWithTag (2);
textfield.ResignFirstResponder ();
nextResponder.BecomeFirstResponder ();
} else if(textfield.Tag == 2){
textfield.ResignFirstResponder ();
loginButton.SendActionForControlEvents (UIControlEvent.TouchUpInside);
} else {
// Not found, so remove keyboard.
textfield.ResignFirstResponder ();
}
return false; // We do not want UITextField to insert line-breaks.
}
I set some tags on each textfield. Than I use the delegate usernameText.ShouldReturn += TextFieldShouldReturn; to call my method TextFieldShouldReturn. This method selects the next textfield. To jump to the next text field you first dismiss the keyboard with resignFirstResponder and present the keyboard with becomeFirstResponder. With this code the UIKeyboardDidShowNotification is called like the user would tap on the textfield (= simulated tap).
I'd like to achieve the following behaviour of a single UITextField and the keyboard:
when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):
- (void)viewDidLoad
{
[self.editField becomeFirstResponder];
self.editField.delegate = self;
}
Now user inputs some text and presses the return key. That text is added to the data source in the following method:
- (IBAction)didEndOnExit:(id)sender {
//add self.editField.text to data source
}
Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.
This is how I return to the previous view in the app (using a button):
- (IBAction)backButtonPressed:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{}];
}
I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.
You should override the below mentioned method and do whatever you want and showing the keyboard too.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Have a try :)
While dismissing view controller all other code executions freezes when (view controller is being dismissed).
Use some delay (performselectorafterdelay). or
completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and
Also execute code on main Thread which includes UI changes or updates
after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..
From the Doc
Return Value: YES if the text field should implement its default
behavior for the return button; otherwise, NO.
For EX - Use the Delegate of UITextField :
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//add self.editField.text to data source
textField.text = #"";
return NO; // This will do your need
}
Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.
Did anyone notice that UITextField calls textFieldDidEndEditing after clear button is pressed but text property still has old data ?
I'm not sure what code-sample I can provide here. I'm using storyboard if that matters.
For now I have to rely on taking data from all edit controls on main form's "Submit" button. But ideally I'd prefer to collect data in textFieldDidEndEditing handler.
Are there any better workarounds ?
I'm on iOS 6.
Update: Basically here is what I have on the form
UITextField and UiButton are on the form.
Keyboard dimissed by calling resignFirstResponder in handler of UITapGestureRecognizer
Steps to reproduce the issue:
Click on edit control. Enter some text.
Tap outside of text control.
textFieldDidEndEditing is called. Property .text has value I entered. All good.
Click on edit control again.
Click on clear button.
textFieldDidEndEditing is called again. But property .text still has value I just deleted !
Now as you see cursor blinking inside UITextField tap on Button on the form.
Keyboard is dismissed by textFieldDidEndEditing was never called.
I'll upload sample project on GitHub tomorrow.
I ran into the exact same problem. In my case, at least, it was due to having added a UITapGestureRecognizer to self.view (to allow for dismissing the keyboard if tapping outside of a UITextField) and setting cancelsTouchesInView=NO on the gesture recognizer. I had set that property in order to get hyperlinking working on a TTTAttributesLabel I have elsewhere in the View.
My workaround was to watch for keyboard show and hide notifications, and toggle that property accordingly:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
(sign up for notifications)
- (void)keyboardDidShowNotification:(NSNotification*)notification
{
tapGestureRecognizer.cancelsTouchesInView = YES;
}
- (void)keyboardDidHideNotification:(NSNotification *)notification
{
tapGestureRecognizer.cancelsTouchesInView = NO;
}
(handle notifications)
The only problem, behavior-wise, is that the hyperlink still doesn't work when the keyboard is displayed: touching it will simply dismiss the keyboard, not forward the touch to the link handler. But I can live with that. After the keyboard is dismissed, the link works fine.
First check UITextFieldDelegate is assigned or not, then
implement the textFieldShouldClear delegate and write the code here clear your textField
To do this you have to set the clearButtonMode property,
yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
yourTextField.delegate = self;
Then implement the textFieldShouldClear delegate
.h file
#interface myViewController: UIViewController <UITextFieldDelegate>{
}
.m file
-(BOOL)textFieldShouldClear:(UITextField *)textField
yourTextFeild.text = #"";
return YES;
}
try here:
-(BOOL) textFieldShouldReturn:(UITextField*) textField
We're trying to figure out how to get the keyboard to hide, but we're having problems getting the textFieldShouldReturn to fire. Why?
This is what has been done:
*.h
#interface MultiSalesViewController : UIViewController <UITextFieldDelegate>
*.c
txtCardNumber.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
return YES;
}
Also, the textField has its delegate set to Files Owner in Interface Builder. One odd thing, is that the viewController's - (void)textFieldDidEndEditing:(UITextField *)textField is working.
How to get the hiding of the keyboard working?
I had the exact same issue and it was because I forgot to set the delegate for the text field in interface builder to 'files owner'.
I had the same problem and, as Warren Crowther suggested, I managed to solve it by holding down CTRL and dragging from the TextBox to the "File's Owner" label.
(Gosh, I miss Visual Studio sometimes...!!)
(Apologies for repeating what's already been said, but I thought a screenshot might be useful !)
I had the delegate set and everything. But I was using a UITextView instead of UITextfield...
Perhaps this will help someone trying to figure why delegate methods are not fired.
I see you put it in your code, but for future visitors, add this to your code:
yourTextField.delegate = self;
I think you are using xib. If so You also need to set delegate over there. Do Right Click on your UITextfiled in xib and you will have delegate option drag it to your file owner.
be sure that your MultiSalesViewController implements the UITextFieldDelegate protocol:
#interface MultiSalesViewController : UIViewController <UITextFieldDelegate>
try adding [self becomeFirstResponder]; after [textField resignFirstResponder];
edit:
just another thought.. does your UITextField have a value set for returnKeyType?
txtCardNumber.returnKeyType = UIReturnKeyDone;
i'm not sure if this has to be set for the function to work
following is answer from Mike Gledhill and Warren Crowther updated with xcode 5 screenshot.
(to set UITextField delegate, press and hold ctrl + drag from the UITextField to the "File's Owner" yellow button, shown in image below. if UITextField delegate not set, textFieldShouldReturn method never gets called).
I had everything wired up just right and - (BOOL)textFieldShouldReturn:(UITextField *)textField was still not being called!
As a work around I configured a method to fire on 'EditingDidEnd':
Go to Connection Inspector and connect delegate to view controller .thats it .
Checklist to get it to work:
Did you set your controller as delegate to UITextField Instance?
Make sure controller is not being deallocated by either assigning to property (Autorelease) or explicit retaining it.
I am running into an issue where the keyboard does not get dismissed when leaving a UITextField or UITextView in a UIModalPresentationFormSheet. In addition, I've created a large button to serve as the view's background so if the user taps outside the fields it gets triggered. I am using the same code in a regular view controller, and it works as expected. In the modal view controller it does nothing. Any suggestions would be appreciated.
- (BOOL)textFieldShouldReturn:(id)sender {
[titleTextField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldReturn:(id)sender {
[synopsisTextView resignFirstResponder];
return YES;
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)textViewDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundClick:(id)sender {
[titleTextField resignFirstResponder];
[synopsisTextView resignFirstResponder];
}
Overriding disablesAutomaticKeyboardDismissal to return NO as below fixed the same problem of mine. You should put this code to your view controller, from which you initiate the keyboard:
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
Also, check this SO question if you want to get a detailed explanation.
For those having trouble with UINavigationController, I think there is a better solution than a category on UIViewController. We should change the behavior of UINavigationController to ask its topViewController (in my opinion, this is how all ViewController containers should handle this).
#implementation UINavigationController (DelegateAutomaticDismissKeyboard)
- (BOOL)disablesAutomaticKeyboardDismissal {
return [self.topViewController disablesAutomaticKeyboardDismissal];
}
If you're presenting a modal view with presentation style "form sheet", Apple apparently does not dismiss the keyboard, thinking that they don't want the keyboard to jump in and out where a user will be doing a lot of editing (i.e. "forms"). The fix would be to change presentation style or live with it.
If you implement the UITextFieldDelegate protocol you can inadvertently cause this behavior if you do text validation. If your validation codes returns false from textFieldShouldEndEditing when the text is invalid, the field can't relinquish it's firstResponder status and the keyboard will remain on screen in the next view.
More details at UITextField's keyboard won't dismiss. No, really
I solved this by resizing a UIModalPresentationPageSheet. See my answer here.
The disablesAutomaticKeyboardDismissal refused to work for me on iOS 7.
But... I managed to solve this issue by simply disabling the UITextFields on the screen.
My solution is described here.
This workaround even works on Modal UIViewControllers.
Yeah... it surprised me aswell !!
i have also facing same problem and also done everything but not thing works then i start thinking and get some result.
but this answer for those who want to dismiss keyboard on textfield click and then open pop up.
so all you need to call text field delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == self.myTxtFieldName{
self.view.endEditing(true) // keyboard hide code
// here you can call your model or pop up code and keyboard will dismiss and your pop up open
return false
}
return true
}
Sorry if this is not working for you
if there is other answer then please edit it
Thank you