自定义UIView的活动 [英] Events for custom UIView

查看:112
本文介绍了自定义UIView的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的UIView子类注册事件的最佳方法是什么,以便我可以将它们连接到界面构建器中的IBAction-s?

What's the best way for registering events for my UIView subclass, so that I can connect them to IBAction-s in interface builder?

目前我刚刚获得一个标准的UIView放到我的主视图上,我把类设置为RadioDial(我的自定义类)。这显示视图很好,但我不知道如何从中获取事件。

Currently I've just got a standard UIView dropped onto my main view and I've set the class to "RadioDial" (my custom class). This displays the view fine, but I have no idea how to get events out of it.

谢谢

推荐答案

请澄清一下:您是否希望Interface Builder提供视图控制器来连接您的视图子类将发出的自定义事件(就像Button控件允许您连线一样)触摸内部等)?

Please clarify: do you mean that you would like Interface Builder to offer your view controllers to wire up custom events that your view subclass will be emitting (much like the Button controls allow you to wire up Touch Inside, etc)?

如果您需要此类功能,则需要在View上使用通用的委托属性并结合协议。

If you need this type of functionality, you will need to use a generalized 'delegate' property on your View combined with a protocol.

@protocol RadioDialDelegate
-(void)dialValueChanged:(id)sender
@end

@interface RadioDial
{
    id<RadioDialDelegate> radioDelegate;
}
@property (nonatomic, assign) IBOutlet id<RadioDialDelegate> radioDelegate;

这将允许控制器连接到视图(假设它实现RadioDialDelegate)并接收任何事件从观点出来。或者,您可以使用无类型委托,并在您的View代码中使用后期绑定调用:

This will allow the controller to wire up to the view (assuming it implements RadioDialDelegate) and receive any events that come out of the view. Alternatively, you can use an untyped delegate and in your View code, use a late bound call:

if([radioDelegate respondsToSelector:@selector(dialValueChanged:)]) {
    [radioDelegate dialValueChanged:self];
}

这篇关于自定义UIView的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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