我有这个 UIView 的自定义子类,称为 productCardView,它非常简单,有一些 UIImageView 和 UILabels 因为它是 subview 。我添加了 subview 并将它们设置为 - (void)drawRect:(CGRect)rect 方法,一切都很好。
在我的 View Controller 中,我从远程服务器获取了一些数据,因此填充了应该可见的 productCardView。目的是当用户点击这些卡片中的每一张时,程序将转到目标 url(每张卡片的 NSURL 属性)。
问题是 根据 MVC 的基础知识,我应该在 View 中添加一个 UITapGestureRecognizer,在我的 productCardView 实现中还是在我的 View Controller ?
如果我要将它添加到 View Controller 中,基本上我会将适当的代码放在 viewDidLoad 方法中,我在其中创建卡片实例,但万一我应该在 View 本身中实现它,< b="">我应该把代码放在哪里? (在 -(void)drawRect:(CGRect)rect 中?)
最佳答案
无论点击是在卡片上还是在 View Controller 上,您都应该从 View Controller 加载 url。
所以,这意味着...
或
如果卡片是在多个地方使用的控件(或者如果屏幕上有多个控件),您最好将 productCardView 设为 UIControl 的子类,而不是UIView。 (UIButton、UISlider 等都是UIControl 的子类)。
您不需要做太多更改,但您可以执行类似...
[productCardView addTarget:self action:@selector(cardTapped:) event:UITouchUpInside];
就像一个按钮。
然后您在卡片 View 中处理触摸并触发事件 UITouchUpInside 的操作。
关于ios - MVC : should i add and implement touch gestures in the View Controller or my custom subclass of UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715707/