在 UISearchBar 上设置启用ReturnKeyAutomatically

发布于 2024-09-02 08:44:23 字数 1142 浏览 3 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-09-09 08:44:23

您正在访问 UISearchBar 的记录视图层次结构。这可能会导致拒绝,但您的应用程序的行为将在固件升级时未指定。

这是一个例子。当该回复发布时,UITextField 仍然是搜索栏的第一个子视图。现在第一个子视图成为 UISearchBarBackground。

最小的改变是迭代整个视图层次结构并找到实际的 UITextField。

for (id subview in searchBar.subviews) {
   if ([subview respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)]) {
      [subview setEnablesReturnKeyAutomatically:NO];
      break;
   }
}

但为了向前兼容,最好使用 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.

for (id subview in searchBar.subviews) {
   if ([subview respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)]) {
      [subview setEnablesReturnKeyAutomatically:NO];
      break;
   }
}

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).

你的心境我的脸 2024-09-09 08:44:23

实际上你可以设置 searchBar.enablesReturnKeyAutomatically = NO;

Actually you can just set searchBar.enablesReturnKeyAutomatically = NO;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文