iPhone keyboard doesn't appear when entering a UITextField
This has to be some kind of newbie blunder that I just can’t see, and I’d be grateful for hints as to what to check or where to look.
I've followed an iPhone tutorial that has a UITextField, making sure I connected the IBOutlet for the text field, and it seems to compile properly (no errors or warnings). But when I run it under the simulator, and clic开发者_如何学Pythonk in the field, I don’t get the keyboard, so I can’t enter anything into the field.
I’ve tried searching the site for similar questions, and all I’ve found is a few questions where the developer is trying to set up some complex UI with multiple controllers, and one that seemed to be the same issue, but the original poster simply said that he solved it by starting a new project and porting the code over. I’d like to find an actual solution, so I don’t have to try randomly rebuilding projects when this issue comes up again.
Thanks!
Just to cover all your bases when troubleshooting this, make sure it's not the iOS Simulator that's causing this. I wasn't seeing the keyboard appear when entering a UITextView as well and it turns out the simulator allows you to toggle between displaying the virtual keyboard and just having you use your laptop's keyboard.
You toggle between the two via:
Hardware
> Keyboard
> Toggle Software Keyboard
or press Cmd + K.
(my source: https://stackoverflow.com/a/24401535/1700540)
So make sure you didn't mistakenly toggle the simulator to only use your physical keyboard! ... like I had
Make sure parent containers have User Interaction Enabled checked. It is not sufficient for the individual controls to have this checked. The UIView can inadvertently get this deselected.
One thing you should check is to make sure the containing view (the UIView that contains all the controls), which is the View icon in Interface Builder nib viewer, has User Interaction Enabled checked. If the parent container doesn't have this checked, even if the individual controls do, you will see this behavior.
What is the delegate for the UITextField connected to? Have you made sure that the delegate functions, particularly - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
is returning the right thing (in this case YES)?
Not sure if this is anyone else's problem but I was using a TextField in a TabBar controller displayed modally and I added the shake gesture in my main view controller. This unfortunately meant I was setting [self becomeFirstResponder] in the viewDidLoad. In that case when my modal view displayed it was not becoming the first responder and hence the keyboard did not display.
To fix it I added [self resignFirstResponder] just prior to calling the modal display and all is well once more! Yay (only took five days to figure that out).
Basicly the only thing you need to do is declare the text field in the .h
IBOutlet UITextField *textField
then declare with same name in the property
@property (nonatomic, retain) UITextField *textField;
then make sure you synthesize in the .m
@synthesize textField;
then you have to link the field in the interface builder
精彩评论