我有一个简单的 UIViewController 和A UISearchBar ,当视图加载时,我希望让搜索栏立即成为第一个响应者,以便显示键盘,并且他们可以立即开始键入查询。我试着这么做 viewWillAppear 像下面这样没有任何运气:
UIViewController
UISearchBar
viewWillAppear
- (void)viewWillAppear:(BOOL)animated { [productSearchBar becomeFirstResponder]; [super viewWillAppear:animated]; }
还有别的地方我该打电话吗 becomeFirstResponder 上 搜索栏 或者我应该打电话给其他人吗?
becomeFirstResponder
搜索栏
把这个移到 -viewDidAppear 它应该是好的。 -becomeFirstResponder 拉起键盘(如你所注意的),你不应该在屏幕上之前做动画。你可以得到奇怪的互动。
-viewDidAppear
-becomeFirstResponder
如果它什么都不做,那么几乎可以肯定 productSearchBar 是一个iboutlet,你忘了把它和 UISearchBar 在接口生成器中。这是用户界面中“没有发生任何事情”的1原因。
productSearchBar
请注意,您不应该以这种方式访问IVARS;您应该将其设置为一个属性,并且只引用 self.productSearchBar . 苹果最终在他们的 Memory Management of Nib Objects . 切勿在访问器外访问您的IVARS,或 -dealloc .此规则将为您节省许多调试时间。
self.productSearchBar
-dealloc