在 UISearchBar 上设置启用ReturnKeyAutomatically
我正在为 iphone OS 3.1.3 编写。我希望 UISearchBar 键盘上的搜索按钮始终启用。如果这是任何旧的 UITextField(不是搜索栏),则该属性将为启用ReturnKeyAutomatically。
我尝试使用 http://discussions.apple.com/ 给出的示例进行设置thread.jspa?messageID=8457910
这表明:
UITextField *searchTextField ;
searchTextField = [[searchBar subviews]objectAtIndex:0];
searchTextField.enablesReturnKeyAutomatically = NO ;
应该可以工作。
不幸的是它崩溃了:
2010-05-20 08:36:18.284 ARemote[5929:207] *** -[UISearchBarBackground setEnablesReturnKeyAutomatically:]: unrecognized selector sent to instance 0x3b31980
2010-05-20 08:36:18.284 ARemote[5929:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UISearchBarBackground setEnablesReturnKeyAutomatically:]: unrecognized selector sent to instance 0x3b31980'
我也尝试过
((UITextField *)[(NSArray *)[searchBar subviews] objectAtIndex:0]).enablesReturnKeyAutomatically = NO;</code>
给出类似的结果。
有什么想法吗?
干杯 埃里克
I'm writing for iphone OS 3.1.3. I want the search button in the keyboard from my UISearchBar to have the search button enabled all the time. If this was any old UITextField (not a search bar) the property would be enablesReturnKeyAutomatically.
I have tried setting this using the example given at http://discussions.apple.com/thread.jspa?messageID=8457910
which suggests:
UITextField *searchTextField ;
searchTextField = [[searchBar subviews]objectAtIndex:0];
searchTextField.enablesReturnKeyAutomatically = NO ;
Should work.
unfortunately it crashes:
2010-05-20 08:36:18.284 ARemote[5929:207] *** -[UISearchBarBackground setEnablesReturnKeyAutomatically:]: unrecognized selector sent to instance 0x3b31980
2010-05-20 08:36:18.284 ARemote[5929:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UISearchBarBackground setEnablesReturnKeyAutomatically:]: unrecognized selector sent to instance 0x3b31980'
I have also tried
((UITextField *)[(NSArray *)[searchBar subviews] objectAtIndex:0]).enablesReturnKeyAutomatically = NO;</code>
Which gives similar results.
Any ideas?
Cheers
Erik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在访问 UISearchBar 的记录视图层次结构。这可能会导致拒绝,但您的应用程序的行为将在固件升级时未指定。
这是一个例子。当该回复发布时,UITextField 仍然是搜索栏的第一个子视图。现在第一个子视图成为 UISearchBarBackground。
最小的改变是迭代整个视图层次结构并找到实际的 UITextField。
但为了向前兼容,最好使用 UITextField 而不是 UISearchBar,或者不要要求始终启用搜索按钮(例如使用“取消”按钮)。
You're accessing an the documented view hierarchy of UISearchBar. This may be lead to rejection, but your app's behavior will be unspecified on firmware upgrade.
This is an example. When that reply was posted, the UITextField was still the 1st subview of the search bar. Now the 1st subview becomes UISearchBarBackground.
The minimal change is to iterate through the whole view hierarchy and find the actual UITextField.
But for forward compatibility, it's better to use a UITextField instead of a UISearchBar, or not to demand the search button be enabled all the time (e.g. use the Cancel button).
实际上你可以设置
searchBar.enablesReturnKeyAutomatically = NO;
Actually you can just set
searchBar.enablesReturnKeyAutomatically = NO;