gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:16:02 27 4
gpt4 key购买 nike

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

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,但这会很痛苦,如果我有更多类型的 View ,我必须为每个代码添加几行代码。

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

最佳答案

尝试将其转换为 id:

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

关于objective-c - 如何停止警告 UIView 可能无法响应选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4340888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com