如何停止UIView警告可能无法响应选择器

时间:2010-12-02 22:57:18

标签: objective-c xcode ios compiler-warnings

我有一个UIView作为属性的类。有时我传入UILabel;有时是UITextField。无论我传入哪个,我都希望课程设置文本。目前我正在这样做,这有效:

if ([self.viewToUpdate respondsToSelector:@selector(setText:)] && !self.newAnswer)
    [self.viewToUpdate setText:[[self.choices objectAtIndex:indexPath.row] text]];

问题是,这会发出警告,因为即使我正在检查respondsToSelector,Xcode也不知道我的UIView会回复setText:。如何删除此警告?

我知道我可以专门检查它是TextField还是Label,然后分别转换为TextField或Label,但这会很痛苦,如果我有更多类型的视图,我我必须为每一行添加更多行代码。

我考虑创建自己的协议,然后让我的类将id作为viewToUpdate的类型...但当然UITextField和UILabel不符合该协议......

1 个答案:

答案 0 :(得分:16)

尝试将其转换为id:

if ([self.viewToUpdate respondsToSelector:@selector(setText:)] && !self.newAnswer)
    [(id)self.viewToUpdate setText:[[self.choices objectAtIndex:indexPath.row] text]];