为什么这段代码不再适用于7.1?

时间:2014-03-13 02:36:16

标签: objective-c xcode5

UITextField * textField= self.textField;
textField.background = [UIImage resizeableImageWithCapInsets2:UIEdgeInsetsMake(0, 7, 0, 7) withName:@"Search-Field"];

简单的代码。

2014-03-13 09:31:02.099 isikota[179:60b] -[BGSearchBar setBackground:]: unrecognized selector sent to instance 0x17566b20
2014-03-13 09:32:17.720 isikota[179:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BGSearchBar setBackground:]: unrecognized selector sent to instance 0x17566b20'

没有意义。显然,background是textField的属性

我发现问题是我使用它:

-(UITextField *) textField
{
    UIView * textFieldView = [self findASubViewWithProtocol:@protocol(UITextInputTraits)];

    return (UITextField *)textFieldView;
}

这似乎不再是从UISearchBar获取textField的方法。怎么办呢?

我认为问题出在7.1 [UISearchBar conforms to UITextInputTraits]

之后

我也想知道为什么

return (UITextField *)textFieldView;

不会返回运行时错误,因为现在textFieldView不再是UITextField的子类

1 个答案:

答案 0 :(得分:2)

我总是这样做:

for (UIView* v in self.searchbar.subviews) {
    if ([v isKindOfClass: [UITextField class]]) {
        return v;
    }
}

或者,在iOS 7中:

for (UIView* v in [self.searchbar.subviews[0] subviews]) {
    if ([v isKindOfClass: [UITextField class]]) {
        return v;
    }
}

这不再起作用了吗?