How can you programmatically bring up keyboard with CCTextField? - ios

I have a CCTextField that is hidden until the user taps on an object, which then adds it to the node tree and it's visible.
Currently, the user must then tap the CCTextField to activate the iOS keyboard and begin entering text.
Is there a way to programmatically initiate the keyboard appearing ?
becomeFirstResponder isn't working.

I dug a little deeper into the CCTextField class code, and cocos2D adds the UITextField property as a subview of the cocos2d view.
For some reason though, this didn't work :
[myCCTextField.textField becomeFirstResponder];
This is what I did, and it worked :
[[CCDirector sharedDirector] view]subviews[0] becomeFirstResponder];
If you have multiple CCTextFields you are obviously going to have to determine the index of your desired text field that you'd like to edit.

Related

iOS Custom keyboard can't return focus to app's textfield

I'm working on a custom keyboard for iOS which will have its own search field (similarly implemented by PopKey).
My keyboard's textfield is able to take the focus with becomeFirstResponder and I'm able to give it up by using resignFirstResponder. However after I resign focus, the host app has a difficult time retaking focus despite touching the form. The app's textfield will still show the text cursor blinking.
Any ideas? Thanks
The solution is a hack, as of right now you can't really give the host app its focus back.
Subclass a UITextField and on its delegate implement
textFieldShouldBeginEditing by returning NO.
Add a BOOL property isSelected that gets set to YES in touchesBegan (not to be confused with the default selected property)
In your keyboard's keyPressed method, if searchField.isSelected, manipulate the searchField.text. Else, manipulate textDocumentProxy like normal.
Add a clear button and method that wipes searchField.text and searchField.isSelected, allowing any further keystrokes to return to the textDocumentProxy
Add an animation that replicates the blinking type cursor

iOS: UIControl in UIScrollView not registering click events

I have a UIControl within a UIScrollView. I scrollview has a textfield. I want the text field to loose focus when the UIControl is touched.
This is the method the UIControl is hooked up to:
- (IBAction) clickedBackground
{
[self.view endEditing:YES]; //make the view end editing!
}
But, the UIControl doesn't seem to call this event when touched.
I did try this as suggested elsewhere, but to no avail:
[scroller setDelaysContentTouches:YES];
The touch event on your UIControl is getting conflicted with touch event of your UIScrollView.
If you just to loose focus of the text field you can use on a button tap.
[textField resignFirstResponder];
But ideal way is to loose focus when user taps anywhere outside the text field. This answer would help you to achieve this.
However, if you just want to resign the keyboard. Then make use of resignFirstResponder on a button tap. You can also look into UITextField input accessory view in order to display a button on the keyboard input view that user can tap on.

Is there a way to know that iOS keyboard is really hidden (dismissed by user)?

I need a way to detect the case when user dismisses iOS keyboard manually, using "keyboard" button on keyboard. I tried to use UIKeyboardDidHideNotification, but quickly discovered that this event is also fired when user splits the keyboard, leaving it on screen.
Is there a way to know for sure that keyboard was really hidden?
To get solution I had to slightly modify my original implementation: I've replaced assigning nil to inputView member of my main view with creating and destroying custom invisible UIView<UIKeyInput> view to show and hide keyboard correspondingly. This allowed me to override this view's resignFirstResponder method which is always called on keyboard resigning - either in normal or in split state, when user dismisses keyboard using special button or when I remove it programmatically.
I believe that UIKeyboardDidHideNotification is only sent when the keyboard is truly gone. From the Apple docs:
Posted immediately after the dismissal of the keyboard.
However, you could also look to see if any of your inputs are currently the first responder:
BOOL keyboardUp = NO;
for (UIView *view in self.textInputs)
{
if (view.isFirstResponder)
{
keyboardUp = YES;
break;
}
}

How do I make the keyboard go away when the user clicks somewhere else? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Programmatically show the keyboard on iPhone app

I have a view with only one UITextView that is used to enter a value. I want that when the view shows, the textview becomes the first responder (that's the easy part) and also the default keyboard shows up. I tried searching for this in speca but to no avail. There are many posts on how to dismiss the keyboard, but what I want is to show the keyboard w/o waiting for user to touch my textview.
Just setting the text field to firstResponder should do the trick.
Inside your -viewDidLoad:
[myTextField becomeFirstResponder];

Resources