How to set Custom keyboard specific to only a UItextfield - iphone

How to set Custom keyboard specific to only a UITextField? When I am changing using this method, all the keyboards in my application are changed to this new custom keyboard. I added:
[[NSNotificationCenter defaultCenter] addObserver:self.view
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
in a UIViewController. But after going to that UIView, keyboards in other UIViewControllers also look like new custom keyboard. How can i limit the custom keyboard to only one UIView? Please help me. Thanks in advance.

UITextField* textField;
UIView* customKeyboard;
textField.inputView = customKeyboard;
Similar thread: iPad custom Keyboard GUI

If you subclassed UITextView (as that tutorial shows), then all instances of that subclass will use the toolbar with a dismiss button.
If you don't want the toolbar, then don't use the subclass, just use the original UITextView.

You can try checking for the textfield that you want on the textFieldShouldBeginEditing like so:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == YOUR_DESIRED_TEXTFIELD) {
[self openCustomKeyboard];
}
return YES;
}

My problem was once am loading custom keyboard, it remains everywhere in other UIviews of application. So i checked existence of UIToolbar in other UIkeyboard subviews and removed . Now its working fine..
for(UIView* keyboardToolbar in [keyboard subviews]){
if([[keyboardToolbar description] hasPrefix:#"<UIToolbar"] == YES)
{
[keyboardToolbar removeFromSuperview];
}
}

Related

Hide keyboard on ViewWillAppear

i have a screen having navigation controller and text field. when i move next and come back i want the keyboard should be hidden in first screen. I am hiding keyboard like on textfield event.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
But how to do that in View related events so that whenever my view appears keyboard is hidden..
Pls guide/Help.
thanks in adv.
I think this is also a good way to remove keyboard with in iOS App if your UITextView or UITextField not connected through the IBOutlet.
If you want to Hide Keyboard with UIViewController LifeCycle Events like with viewWillAppear or etc. Follow this
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self view] endEditing:YES];
}
Otherwise if you object connected using IBOutLet this code will work fine as you describe too.
[yourTextField resignFirstResponder];
Add this code to your ViewWillAppear :
for(id obj in self.view.subviews)
{
if([obj isKindOfClass:[UITextField class]])
{
[obj resignFirstResponder];
}
}
This would take in all the textfields in that particular view here it is the whole view and add the code you had written previously for removing the keyboard.
A good habit is to write this code in your screen's -viewWillDisappear. So, when you navigate from one screen to another at that time it will remove the keyboard from that screen.
- (void)viewWillDisappear:(BOOL)animated
{
[self.view endEditing:YES];
[super viewWillDisappear:animated];
}
For multiple textFields, it is better to use -endEditing for that particular view instead of -resignFirstResponder for any single textField. Take a look at my Answer.
//This is for Swift
override func viewWillAppear(animated: Bool)
{
self.view.endEditing(true)
}
The thing that you are doing wrong is , when you are moving back previous controller to the current controller , the keyboard is up due to the selected textfield of previous controller .
And in the current controller the code:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self view] endEditing:YES];
}
It will not work as no textfield is selected at this controller. So what you need to do is write the same code in the previous controller viewWillDisappear Method it will surely resolve your Problem .
- (void)viewWillDisappear:(BOOL)animated
{
[self.view endEditing:YES];
[super viewWillDisappear:animated];
}

How to call method on return key and also on ipad down arrow key

I have method for textfield animation which is running on after resignfirsrresponder i want that method also be called when user presses down arrow of the ipad which hides keyboard any idea how to do this. thanks.
I have added screenshots the button beside ABC text button i when we press that button normally keyboards hides i want to call animation on that button click.
For Ipad down arrow key You can use notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
-(void)keyboardWillHide
{
//call when key board hides
}
You can call the textfield animation method in any one of the following UITextFieldDelegate methods
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
or
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
just write your code inside
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
And don't forget to bind textfield delegate and write <UITextfieldDelegate> in .h file
if you want use done button on key board take one view with done button and write this method
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setInputAccessoryView:self.doneView];
}
and then give following action to that done button
-(IBAction)doneButtonPressed:(id)sender
{
[currentTextField resignFirstResponder];
}

UIKeyboardWillHide not triggered

I read many post here about this topic, but I wasn't able to find an answer to my question, so, hope you won't be bored about another UIKeyboard post :-)
In my view controller's implementation I added self as an observer for the two notifications UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, passing the selectors keyboardWillShow: and keyboardWillHide: to handle to notifications. As I touch a UITextField, the keyboardWillShow: method is called but when I press a "Done" button (which dismisses the keyboard) the keyboardWillHide: method is not called.
Really, I'd like to make my UITextField show a keyboard with the "hide button" on the bottom right of the keyboard, but I wasn't able to find the right keyboard type. Maybe I need to set the textfield retuntype to "...Done". In that way I saw that "return" key turns to "done".
So I set a toolbar to be my UITextField's inputAccessoryView, so now I can show a standard keyboard with a tool bar above with the "Done" button. As a user touches that button, I hide the keyboard with the resignFirstResponder method.
The strange thing is that when I call resignFirstResponder, the UIKeyboardWillHideNotification isn't posted; at least the keyboardWillHide: method is not called.
What do you suggest to me? I really wanted to display a keyboard with the small button with the down arrow to hide the keyboard, but also this solution could be right, but I'd like to resize the view and to do this I need to observer UIKeyboardWillHideNotification.
Thank you very much for help...
(ADDED:)
In viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:[[self view] window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:[[self view] window]];
I took these declarations from one of "yours" post :-) But the willShow works...
The action of the "Done" button that's in the UIToolbar that's assigned to be the inputAccessoryView of my text field is:
-(void)keyboardDone {
[msgTextField resignFirstResponder];
CLOSED:
OK! When a developer is stupid... it is stupid :-) :-)
This is my corrected willHide method:
-(void)keyboardWillHide:(NSNotification*)n {
NSDictionary* userInfo;
CGSize keyboardSize;
CGRect viewFrame;
/* This was the bad guy :) I forgot to delete it
* after I previously copied the willShow method that
* checks if keyboard is already shown (if so returns).
*
* if( keyboardIsShown )
* return;
*/
userInfo = [n userInfo];
keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
viewFrame = [[self scrollView] frame];
viewFrame.size.height += ( keyboardSize.height - TABBAR_HEIGHT );
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
[[self scrollView] setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
NSLog(#"HIDE\n");
}
First of all I'd like to thank you all for this useless work in helping me. I'd like to give you some points, so I'll try to rise a "interest point" for each answer, but I need to choose the right one... hard part... :-)
Excuse me again... I really didn't see the if() statement...
If you read the documents for UIWindow it says that the notification object for these notifications is nil. You are passing self.view.window in as the object to the addObserver:selector:name:object: method. Try passing nil instead:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
It's important to note that when the user hides the software keyboard via the hide button, the hide methods aren't called. The show methods are called again, but the keyboard is nearly off screen except for the home row toolbar.
Check, if keyboardDone really gets called (i.e. with NSLog(#"%#", #"keyboard done called");). If its get called, but resignFirstResponder does not help dismissing the keyboard, then try this:
[self.view endEditing:YES];
Please also provide your keyboardWillHide: method.
To set the keyboard up so that it has a "Done" button, do this:
1) Setup your view controller so that it implements the UITextFieldDelegate. For Example:
#import <UIKit/UIKit.h>
#interface TX_ViewController : UIViewController <UITextFieldDelegate>
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
2) In your view controllers implementation file, use the following code to setup the keyboard:
- (void)viewDidLoad
{
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
[self.textField addTarget:self action:#selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
3) And if you wish to do something when the DONE button is pressed, simply add the following function to your view controller's implementation file:
- (IBAction)textFieldFinished:(id)sender
{
[sender resignFirstResponder];
}
Also, if you are using Interface builder to create your interfaces, don't forget to setup your IBOutlet reference for the TextField; otherwise, your class won't receive the messages from the XIB.
I set this up in a sample application just to see if it works and it did perform in the way you wish for your application to perform.
Swift $
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
}

Creating a new keyboard button for iPhone - Bug in InputAccessoryView and InputView

I create a 2 UITextView in my program. They will have to do this:
TextView1 - Will have a new InputView
TextView2 - Will be a normal numeric keyboard with a new button
Because the numeric keyboard of iPhone have a empty button in left of zero. So, I can put a UIView there to simulate a new keyboard. So, I created a UIView with a UIToolbar and this button and set in TextView with a simple
[textView setInputAccessoryView:myView];
To catch a click inside this view is a little more complex. I have to subclass the principalClassName in main.m
int retVal = UIApplicationMain(argc, argv, MyPrincialClass ,MyDelegateClass);
Then create this class with
#interface MyPrincialClass : UIApplication
- (void)sendEvent:(UIEvent *)anEvent;
#end
#implementation MyPrincialClass
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *touches = [event allTouches];
UITouch *touch = touches.anyObject;
[[NSNotificationCenter defaultCenter] postNotificationName:#"click" object:(id)touch];
}
#end
Now is only listen this Notification and check if this click is in correct area.
Because I have a UINavegationBar, I have to go to "Next" and "Previous" TextView. OK, this is only run
[textView1 resignFirstResponder];
[textView2 becomeFirstResponder];
If all textView have the same inputView (the keyboard) than the button work normaly. But... Because one of this textView have another inputView, than my problem start.
If I first click in a normal UITextView with a button, than the button is show correct like this. The red square is above the keyboard, so it work.
If the first click is in the UITextView with a customized inputView, the button is show correct again
but now, If navegate with the tool bar (with is a simple resignFirstResponder and becomeFirstResponder) the button will be show under the keyboard
In resume... All the problem in only because I use a custom inputview. If I only use the keyboard, I will not have any problem.
To solve this, is only put the UIView of custom button above the keyboard. I try to get the main UIWindows and send the message:
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:myview];
But not work. Any idea to solve this bug??
I Hope i understand you correctly, and i would suggest you to manage Z Position of the button. So that It can appear above all the component. For easy doing i suggest you the following open source,Manage Z Order of Views.
Hope this will help you, if not please let me know .
Cheers,
Arun.
I fix it. The idea was find the UIView of keyboard and add the button like a subview. This was the codes:
- (void)keyboardDidShow {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(UIView* potentialKeyboard in tempWindow.subviews) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[potentialKeyboard description] hasPrefix:#"<UIPeripheralHost"] == YES) {
keyboard = potentialKeyboard;
} else {
if([[potentialKeyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
keyboard = potentialKeyboard;
}
}
}
}
[keyboard addSubview:key];
[keyboard bringSubviewToFront:key];
}

How to cancel the keyboard in UISearchbar?

I'm using UISearchBar in my application and it serves as both an edit field as well as a search. So when I want to disappear the keyboard I have to use cancel button in UISearchBar but I can't have that cancel button on screen, so how could T make keyboard disappear when not used without using cancel button. Please help me as i'm new to iPhone application development.
Thanks in advance!
Use this:
[UISearchBar resignFirstResponder];
Just replace the word UISearchBar with the name of the object you have created.
Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? If programmatically, then [UISearchBar resignFirstResponder]. If you are looking for a possible way for the user to achieve that you can either make the return button on the keyboard resign its first responder status when pressed, or attach a UIGestureRecognizer to your view and set it up so that when the user clicks outside the keyboard, this keyboard goes away.
simply all you need to do is to get UITextfield control from the UISearchbar and then set UITextfield's delegate to whatever delegate that will perform -(void) textFieldShouldReturn:(UITextField *)textField
-(void)viewDidLoad{
UIView * subView;
NSArray * subViews = [searchbar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return TRUE;
}