将实例变量的 setter/getter 传递给类
我正在创建一个 UIView 子类(我称之为 MarqueeLabel ),当 UILabel 文本对于包含的视图来说太长时,它会以字幕方式为子视图 UILabel ivar 提供动画效果。
我希望有一个干净的实现,而不必在我的 MarqueeLabel
类中编写方法只需来设置/检索所有标准 UILabel(文本、字体、颜色等) ) UILabel ivar 的实例变量。我找到了一种通过消息转发来实现此目的的方法 - 发送到 MarqueeLabel 的所有无法识别的方法都会传递到 UILabel ivar。就我而言,MarqueeLabel 无法识别的方法是通常与 UILabel 一起使用的方法。
但这种方法存在一些问题:
1. 您必须使用[marqueeLabel setText:@"Label here"]
,而不是marqueeLabel.text
2.编译器在上面的行中给出警告,因为:
“MarqueeLabel”可能不会响应“-setText:”
,我知道要忽略它,但会惹恼其他人。
为了避免这些问题,有没有什么方法可以“提出”ivar 的方法,以便使用该类的东西可以访问它们,同时仍然作用于 ivar 对象?
谢谢!
注意:我设置的方式可能也不是最好的方式。也许子类化或类继续 UILabel 会更好,但我无法理解如何使用这些方法来完成动画+剪辑(当滚动的文本移出包含 UIView 并消失时)。
注2:我知道您可以使用marqueeLabel.subLabel.text
,其中subLabel
是子视图UILabel。这可能是我采取的方向,但不妨看看是否有更好的解决方案!
I'm working on creating a UIView subclass (which I'm calling MarqueeLabel
) that animates a subview UILabel ivar in a marquee fashion when the UILabel text is too long for the containing view.
I was hoping to have a clean implementation, without having to write methods in my MarqueeLabel
class just to set/retrieve all the the standard UILabel (text, font, color, etc) instance variables of the UILabel ivar. I've found a way to do this with message forwarding - all unrecognized methods sent to MarqueeLabel
are passed on to the UILabel ivar. In my case the methods unrecognized by MarqueeLabel
are the methods typically used with UILabel.
There are some problems with that approach though:
1. You have to use [marqueeLabel setText:@"Label here"]
, rather than marqueeLabel.text
2. The compiler gives warnings on the above line, because:
'MarqueeLabel' may not respond to '-setText:'
which I would know to ignore but would annoy anyone else.
To avoid these problems, is there any way to "bring forward" the methods an ivar so that they're accessible to something using the class while still acting upon the ivar object?
Thanks!
Note: The way I've set this up may not be the best way to do it either. Perhaps subclassing or class continuing UILabel would be better, but I wasn't able to grasp how the animation + clipping (when the text scrolling off moves out of containing UIView and disappears) could be done using those methods.
Note 2: I know you can use marqueeLabel.subLabel.text
where subLabel
is the subview UILabel. And this may be the direction I take, but might as well see if there's a better solution!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于属性,您可以在接口中定义属性并在实现中使用 @dynamic,这样您就不必创建存根实现。确保您还覆盖
valueForUndefinedKey:
和setValue:forUndefinedKey:
并转发到您的标签。对于不属于属性一部分的任何方法,您可以使用类别来声明该方法而不实现它。这将消除警告,但仍使用内置转发。
For properties, you could define a property in the interface and use @dynamic in the implementation so that you can don't have to create stub implementations. Make sure you also override
valueForUndefinedKey:
andsetValue:forUndefinedKey:
and forward to your label.For any methods which are not part of a property, you can use a category to declare the method without implementing it. This will get rid of warnings but still use the builtin forwarding.