如何停止对UIView的警告可能无法响应选择器 [英] How to stop warning for UIView may not respond to selector

查看:48
本文介绍了如何停止对UIView的警告可能无法响应选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将UIView作为属性的类。有时我会传递UILabel;有时是UITextField。不管我传递什么,我都希望班级设置文本。目前,我正在这样做,它可以正常工作:

I have a class that has a UIView as a property. Sometimes I pass in a UILabel; sometimes a UITextField. No matter which I pass in, I want the class to set the text. Currently I am doing this, which works:

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

问题是,这给出了警告,因为即使我正在检查 respondsToSelector ,Xcode不知道我的UIView将响应 setText:。我如何删除此警告?

The problem is, this gives a warning, because even though I'm checking respondsToSelector, Xcode doesn't know that my UIView will respond to setText:. How can I remove this warning?

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

I know that I can specifically check to see if it's a TextField or a Label, and then cast to a TextField or a Label, respectively, but this would be a pain, and if I ever have more types of views, I'd have to add a few more lines of code for each one.

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

I thought about creating my own protocol, and then having my class have id as the type for viewToUpdate... but of course UITextField and UILabel wouldn't conform to that protocol...

推荐答案

尝试将其强制转换为id:

try just casting it as an id:

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

这篇关于如何停止对UIView的警告可能无法响应选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆