Hide keyboard on any key press in iphone - ios

I want to hide the keyboard when i press any key other than the return key .
For example when the user presses the character 'n' on keyboard , keyboard should disappear.
Please provide me a suitable answer as soon as possible.
Thanks in advance.

You may try UIKeyInput protocol. To catch a insert with insertText
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIKeyInput_Protocol/Reference/Reference.html
Have you used protocols?
EDIT:
Then you can use method
[textField resignFirstResponder];
To hide the keyboard
EDIT2: Protocols
In short, the interface for class that you want to respond to the protocol must be declare as (in YourClass.h):
#interface YourClass:NSObject<UIKitInput>
then, you have to implements the protocol methods in YourClass.m:
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
.....
return NO;
}
- (void)insertText:(NSString *)theText {
...;
}
- (void)deleteBackward {
....
}
Good Luck!

For that, you have to create a custom keyboard or you have to override the Keyboard methods. But you cannot override. Because You do not know, in which method and what code is to sense that key pressing in that frameworks. So better write a custom keyboard anyway.

The keyboard hides when the text field resigns first responder. The textfield delegate is told when a key is pressed (textField:shouldChangeCharactersInRange:replacementString:). If you need any more detail than that, please put a bit more effort into your question and show us what you have tried so far.

Related

Detect input object view type in custom keyboard extension

I am developing a custom keyboard using app extension in iOS 8.
I have a return key, and on press I want to check if input object is an UITextView, then move to next line using the following code: [self.textDocumentProxy insertText:#"\n"];. And if the input object is an UITextField, then dismiss the keyboard this way: [self dismissKeyboard];
Something like this:
- (void) returnKeyPressed
{
if (inputObjectIsTextView)
{
[self.textDocumentProxy insertText:#"\n"];
}
else if (inputObjectIsTextField)
{
[self dismissKeyboard];
}
}
The question is: How can I detect what kind of input view is currently editing?
There is no need to detect input view is a UITextField or UITextView to enter a new line
as described here under heading "API Quick Start for Custom Keyboards"
says
[self.textDocumentProxy insertText:#"\n"]; // In a text view, inserts a newline character at the insertion point
That means no need of detection, You can do in this way
- (void) returnKeyPressed
{
[self.textDocumentProxy insertText:#"\n"];
}
This will execute if inputView is UITextView and not execute if input type is UITextField.
I have create a keyboard and test this before post it here.
You can`t do that , if you look at the documentation ,you see that there is no public API for detecting keyboard Extensions Text Input Object.
From The Doc.
Because a custom keyboard can draw only within the primary view of its UIInputViewController object, it cannot select text. Text selection is under the control of the app that is using the keyboard. If that app provides an editing menu interface (such as for Cut, Copy, and Paste), the keyboard has no access to it
I think this also implies that you cannot access text Input object from the app that are using your keyboard extension.
Try something like this, take a reference of active view as 'inputViewObject', then try
- (void) returnKeyPressed
{
if (inputViewObject.class==[UITextView class])
{
[self.textDocumentProxy insertText:#"\n"];
}
else if (inputViewObject.class==[UITextField class])
{
[self dismissKeyboard];
}
}
Hope it helps.

How to stop the UIKeyboard return key from working

I have a UITextField thats currently in a UITableViewCell, when you select the UITextField the UIkeyboard automatically comes up.
I would like to know how I can just ignore the return key when its pressed.. there is a feature I have no implemented yet to skip UITextFields its causing me some errors and before I preview my app I would like to disable it to avoid any confusion.
Sure, you'll need to use UITextFieldDelegate, specifically, textFieldShouldReturn:. All you have to do is specify that your class will conform to this protocol in your interface, and specify that this class as the delegate on the textfield's delegate property and you're good to go. That is, after returning no from textFieldShouldReturn:. Or of course, this can be done conditionally.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (someCondition == YES) {
return NO;
}
return YES;
}

How do you make the keyboard appear with no UITextField

How would I be able to make the keyboard appear automatically when the viewDidLoad? I also do not want to use the UITextField to make the keyboard appear.
Add a UITextField to your view and call it [myTextfield becomeFirstResponder]; Then set it to hidden myTextfield.hidden = true - so the user will never see the textfield.
You can do it by:
subclassing UIView (which is a subclass of UIResponder)
make your UIView Subclas conform to protocol UIKeyInput. To do this add
< UIKeyInput >
in the class declaration and this code to .m file
Add this code:
-(BOOL)hasText;
{
return YES;
}
-(void)insertText:(NSString *)text;
{
// what to do when a text is inserted
}
-(void)deleteBackward;
{
// what to do when delete is pressed
}
-(BOOL)canBecomeFirstResponder
{
// return yes if your control can become first responder and show the keyboard
return YES;
}
create a new instance of this custom view and add it as a subview of the current window (otherwise it won't work)
then, to show the keyboard, you have to simply call [myView becomeFirstResponder];
Just tried and it works well.
Edit: haven't tried, but it should work subclassing not only uiview, but all kind of objects that are subclasses of UIView and that can be added in the window hierarchy, simply it must conform UIKeyInput protocol
The only way I know without an UITextField is creating and adding an instance of the (private, undocumented) UIKeyBoard class. You can find an implementation here, in the showKeyboard:animated: method.

MFMailComposeViewController iphone keyboard does not have DONE or CANCEL

The keyboard that comes up with MFMailComposeViewController does not have any means to dismiss the keyboard once it comes up.
Does anyone have an idea of changing the keyboard. There are no UITextField exposed as you are actually in mail client at the time.
The mail composer isn't yours to modify, it is a system provided view controller which you are explicitly told not to modify in the docs:
Important: The mail composition interface itself is not customizable and must not be modified by your application. In addition, after presenting the interface, your application is not allowed to make further changes to the email content. The user may still edit the content using the interface, but programmatic changes are ignored. Thus, you must set the values of content fields before presenting the interface.
The cancel button is already there in the top left, what would "Done" do? Send the email? That's in the top right.
The MFMailComposeViewController doesn't have a "Done" button, because it assumes you will use that button as a return key (to make a new line).
If you really wanted to change the button to a "done" button, there is only one way I can think to do it:
Create a new MFMailComposeViewController.
Enumerate through [[mailComposer view] subviews].
Inspect each subview (and subviews of subviews, if required).
When you've found the UITextView that is the body, do the following:
// Get the UITextView from subview inspection
UITextView *textView;
// Declare this instance variable in your class #interface
id <UITextViewDelegate> originalTextViewDelegate;
// Get the original delegate
originalTextViewDelegate = [textView delegate];
// Override the delegate
[textView setDelegate:self];
// Set the return key type
[textView setReturnKeyType:UIReturnKeyDone];
Return YES on -textViewShouldEndEditing. Implement ALL UITextViewDelegate methods, and call originalTextViewDelegate (kind of like calling "super" on a subclass).
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[originalTextViewDelegate textViewShouldEndEditing:textView];
// Important: return YES, regardless of originalTextViewDelegate's response
return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[originalTextViewDelegate textViewDidChangeSelection:textView];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return [originalTextViewDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
}
// etc
That should work, but no guarantees. Hope that helps! :D

textFieldShouldReturn not being called in iOS

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.

Resources