How to pull up a UIKeyboard without a UITextField or UITextView? - iphone

I'm currently developing an OpenGL ES game for the iPhone and iPod touch.
I was wondering how I can easily pull up the UIKeyboard?
Is there an official, documented possibility to pull up a UIKeyboard without using a UITextField of UITextView?

If you subclass UIResponder, and declare the UIKeyInput protocol, the keyboard will appear when you become the firstResponder.
See the UIKeyInput protocol here.
One thing that tripped me up is that you'll need to override the canBecomeFirstResponder message.

It's not "officially" possible - you can't access the UIKeyboard object at all while staying within Apple's guidelines.
Creating an invisible UITextField, then calling [textField becomeFirstResponder] would do the job - you could even subclass UITextField first, then override the delegate method textField:shouldChangeCharactersInRange: to redirect the text input to where you want it to go.

It is indeed possible. I had myself struggled a lot with this. UIKeyInput protocol can be used to pull a keyboard without using a UITextField or UITextView. However it is only available in iOS 3.2 and above. Here is a tutorial on that.
Hope that helps.

I displayed the keyboard without any visible UITextField by positioning my textfield's frame out of the visible screen:
#define TEXT_FRAME -50,-50,0,0
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(TEXT_FRAME)];
When I set the "first responder" the keyboard becomes visible without any visible input area:
// show the keyboard
[self.textField becomeFirstResponder];
I later dropped the idea. However, I don't know its conformance to the Apple guidelines.

I haven't tried it but http://www.cocoadev.com/index.pl?UIKeyboard shows some code that looks like it should work.

Related

Making UITextView behave like UITextField

How do I make a UITextView behave like a UITextField or maybe how to make a UITextField appear like this, same with the photo, i've searched a lot about making UITextField into multiple lines but i can't seem to get how they did it, is there any method that is much simpler,
I also tried putting a UITextView and then sending it at back and putting at top a UITextField and in the viewDidLoad I did self.commentTextField.frame = self.commentsTextView.frame; , i thought it would make the UITextField like a UITextView.
I want that when I start editing this
- (void)textFieldDidBeginEditing:(UITextView *)textField;
delegate would detect that Im editing the UITextField because I can't seem to detect when Im editing the UITextView
As others said, use UITextView instead of UITextField, just remebering of setting editable to YES.
And about the delegate, - (void)textFieldDidBeginEditing:(UITextView *)textField; is a method of UITextFieldDelegate , and, as the name of the protocol says, it will only work for UITextFields. Changing the type of the parameters will have no effect.
What you may want is the similar textViewDidBeginEditing: , of UITextViewDelegate. Implement this new protocol, just don't forgetting to set the delegate correctly.
self.textView.delegate = self;
UITextField is one line only, you need to use UITextView instead, and if you are looking for rounded corners you can use:
self.commentTextField.clipsToBounds = YES;
self.commentTextField.layer.cornerRadius = 10.0f;
I suggest to check this question:
How to create a multiline UITextfield?

why override canBecomeFirstResponder?

I'm looking at TVAnimationGestures from WWDC 2010, and in the TableVieWController.m, they override canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder {
return YES;
}
Is there a reason they do this? I don't see this method called anywhere. Thanks.
So you can mark your question as answered...
They are using an UIMenuController within the sample, and in order to receive messages from that controller to your controller, you must make your controller the first responder (and accept becoming first responder via canBecomeFirstResponder.
This method is called by the Cocoa framework and not typically application to see if a controller should become the first responder. While I haven't looked at that specific example, it probably allows the table to be editable.
I needed to override canBecomeFirstResponder in a custom UIView so I could use a custom InputView and InputAccessoryView.
Custom Views for Data Input
I had to do it this way because if I used a UITextField or UITextView, a hardware keyboard would subvert the more limited on-screen keyboard.

how do I know when UIKeyboard Join button is tapped?

How do I know when a user taps on the Join button on the UIKeyboard?
Also can this text be changed? I know there are several built options but I nee done that says 'Login' if I can.
I'm not a big fan of using UITextField delegate just for knowing when keyboard entry is completed by pressing the "return" on the keyboard (whatever it's labeled). UITextField actually works well with a Target/Action pattern which is much simpler and far more direct than using the delegate for this.
I've pointed this out before in an answer to "How to call a method when the Done Button in the KeyBoard is Clicked?". Please take a look at that for the details of using the traditional target/action pattern to know when keyboard entry is complete. Unfortunately, Apple's documentation isn't as clear is it could be on this subject.
UITextField delegate has its place, but if all you need is to know when entry is complete then using the delegate likely isn't the best approach. Use target/action.
For the UIReturnKeyType choices, see the returnKeyType property in the UITextInputTraits Protocol Reference.
You should set your delegate of your UITextField (I'm assuming that's a UITextField) and check for that delegate call :
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Also, you return a BOOL to tell the UITextField to do its default behavior or not. You can check the documentation here.
Also, the options Apple gives you for the UIReturnKeyType are your only options.

UITextView does not seem to implement reloadInputViews

Loading a custom input view into a UITextField, I can arbitrarily change the keyboard type of the standard keyboard between UIKeyboardTypeAlphabet and UIKeyboardTypeNumberPad. Just call something like:
[editingField setKeyboardType:UIKeyboardTypeNumberPad];
[editingField reloadInputViews];
And viola! There is your number pad.
Without changing this block of code, but just making editingField a UITextView instead of a UITextField, it no longer works. According to the docs, these are both compliant with the UITextInput and UITextInputTraits protocols.
It is worth mentioning that the above code actually does work, but only after the user leaves the textView in question and later reselects it. It is almost like reloadInputViews does nothing, and then the textView loads its input views when it becomeFirstResponder.
I have tried all sorts of performSelector: etc and cannot force the issue. Any ideas? How do you make UITextView obey reloadInputViews and dynamically change its inputView?
Are you setting inputView of UITextView to nil before calling?
[editingField setKeyboardType:UIKeyboardTypeNumberPad];
[editingField reloadInputViews];

How to stop the UITextField from responding to the shake gesture?

By default, if you shake the iPhone while entering text in a UITextField, a little box will appear asking if you want to undo typing. How can I prevent this from happening? I looked through the UITextField, UITextFieldDelegate, and UITextInputTraits docs and found nothing relating to shaking.
Presumably I could subclass UITextField to ignore the shake event, but I'm not exactly confident in my ability to not screw other things up. I'd love something like
textField.respondsToShake = FALSE;
What's the best way to do this?
[UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;
You could try to subclass UITextField and override canPerformAction:withSender: in such a way that it will disallow the undo: action. But then you lose undo both by shaking and through the context menu.