I have a simple UIViewController
and a UISearchBar
, when the view loads I want to have the search bar become the first responder right away so that the keyboard is showing and they can start typing their query right away. I tried doing it in viewWillAppear
like bel开发者_JAVA技巧ow without any luck:
- (void)viewWillAppear:(BOOL)animated
{
[productSearchBar becomeFirstResponder];
[super viewWillAppear:animated];
}
Is there another place that I should be calling becomeFirstResponder
on the UISearchBar
or should I be calling something else entirely?
Move this to -viewDidAppear
and it should be fine. -becomeFirstResponder
pulls up the keyboard (as you note), and you shouldn't do animations before you're onscreen. You can get weird interactions.
If it's not doing anything at all, then almost certainly productSearchBar
is an IBOutlet and you've forgotten to actually tie it to the UISearchBar
in Interface Builder. This is the #1 reason for "nothing happens" in UI.
Note that you shouldn't be accessing your ivars this way; you should make it a property and refer only to self.productSearchBar
. Apple has finally posted a correct explanation of this in their Memory Management of Nib Objects. Never access your ivars outside of an accessor or -dealloc
. This rule will save you many hours of debugging.
精彩评论