调用“[UISearchBar setReturnKeyType:]”时无法识别的选择器

Unrecognized selector when calling `[UISearchBar setReturnKeyType:]`

在我们的应用程序中,我们开始为 iOS 7.0、7.0.4 和 7.0.6 用户收到以下错误:

-[UISearchBar setReturnKeyType:]: unrecognized selector sent to instance 0x178a7920

我们花了一些时间才弄清楚发生了什么,因为 Xcode 6.4 不再支持 7.0、7.0.4 和 7.0.6 模拟器。下面分享解释。

我们从 UISearchBar.h 中的文档中了解到:

...UISearchBar officially conformed to UITextInputTraits in iOS 8.0 and privately conformed in iOS 7.0...

所以 8.x 对 UITextInputTraits 有 public / 完全支持,但是 iOS 的 7.x 版本仅对它有私有/可能的部分支持。

我们开始尝试不同的 iOS 7.x 版本,发现等于或大于 iOS 7.1 的版本支持 setReturnKeyType:,而更早的版本不支持.

似乎a different method在iOS7的早期版本中使用(参见link中第二受欢迎的答案),这似乎验证了我们的结论。

所以对于 iOS 7.x 版本我们最终使用了这个:

if ([searchBar respondsToSelector:@selector(setReturnKeyType:)]) {
    searchBar.returnKeyType = UIReturnKeyDone; // Pick a type
} else {
    // Call the method from the linked answer above for iOS < 7.1
    // or leave the return key type unchanged.
}