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

时间:2015-08-13 19:14:50

标签: ios selector uisearchbar

在我们的应用中,我们开始为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模拟器,我们花了一些时间来弄清楚发生了什么。下面将分享解释。

1 个答案:

答案 0 :(得分:1)

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

  

... UISearchBar在iOS 8.0中正式符合UITextInputTraits并在iOS 7.0中私密地符合......

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

我们开始使用不同的iOS 7.x版本,发现那些等于或大于iOS 7.1的版本支持setReturnKeyType:,而早期版本则不支持。{/ p>

似乎a different method用于早期版本的iOS 7(请参阅链接中第二个最受欢迎的答案),这似乎验证了我们的结论。

因此,对于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.
}