Objective c 如何在UICollection补充视图中使用UIButton?
我试图在UICollectionView补充视图(页脚)中放置UIButton。我已使用故事板将UIButton连接到UICollectionViewCell的子类,并可以通过编程方式更改其属性,包括背景图像。但是,当我将按钮的内部事件连接到一个方法时,它不会触发。事实上,该按钮甚至似乎对用户的触摸没有视觉上的响应。在疑难解答中,我尝试将UIButton添加到集合视图的标题中,并看到相同的行为。将按钮添加到不相关的视图中会在按下时产生交互效果Objective c 如何在UICollection补充视图中使用UIButton?,objective-c,ios,cocoa-touch,ios6,uicollectionview,Objective C,Ios,Cocoa Touch,Ios6,Uicollectionview,我试图在UICollectionView补充视图(页脚)中放置UIButton。我已使用故事板将UIButton连接到UICollectionViewCell的子类,并可以通过编程方式更改其属性,包括背景图像。但是,当我将按钮的内部事件连接到一个方法时,它不会触发。事实上,该按钮甚至似乎对用户的触摸没有视觉上的响应。在疑难解答中,我尝试将UIButton添加到集合视图的标题中,并看到相同的行为。将按钮添加到不相关的视图中会在按下时产生交互效果 要在UICollection补充视图中实现UIBut
要在UICollection补充视图中实现UIButton,我需要做什么特殊的事情吗?补充视图应该是
UICollectionReusableView
(或其子类),而不是UICollectionViewCell
无论如何,如果按钮没有响应,首先要做的是检查它的所有祖先视图是否已将userInteractionEnabled
设置为YES
。按钮出现在屏幕上后,暂停调试器并执行以下操作:
(lldb) po [[UIApp keyWindow] recursiveDescription]
在列表中找到按钮并复制其地址。然后您可以检查它和每个superview。例如:
(lldb) p (BOOL)[0xa2e4800 isUserInteractionEnabled]
(BOOL) $4 = YES
(lldb) p (BOOL)[[0xa2e4800 superview] isUserInteractionEnabled]
(BOOL) $5 = YES
(lldb) p (BOOL)[[[0xa2e4800 superview] superview] isUserInteractionEnabled]
(BOOL) $6 = YES
(lldb) p (BOOL)[[[[0xa2e4800 superview] superview] superview] isUserInteractionEnabled]
(BOOL) $8 = YES
(lldb) p (BOOL)[[[[[0xa2e4800 superview] superview] superview] superview] isUserInteractionEnabled]
(BOOL) $9 = YES
(lldb) p (BOOL)[[[[[[0xa2e4800 superview] superview] superview] superview] superview] isUserInteractionEnabled]
(BOOL) $10 = NO
(lldb) po [[[[[0xa2e4800 superview] superview] superview] superview] superview]
(id) $11 = 0x00000000 <nil>
(lldb)p(BOOL)[0xa2e4800 isUserInteractionEnabled]
(BOOL)$4=是
(lldb)p(BOOL)[[0xa2e4800 superview]isUserInteractionEnabled]
(BOOL)$5=是
(lldb)p(BOOL)[[0xa2e4800超级视图]超级视图]isUserInteractionEnabled]
(BOOL)$6=是
(lldb)p(BOOL)[[0xa2e4800超级视图]超级视图]超级视图]isUserInteractionEnabled]
(BOOL)$8=是
(lldb)p(BOOL)[[0xa2e4800超级视图]超级视图]超级视图]超级视图]isUserInteractionEnabled]
(BOOL)$9=是
(lldb)p(BOOL)[[0xa2e4800超级视图]超级视图]超级视图]超级视图]超级视图]isUserInteractionEnabled]
(BOOL)$10=否
(lldb)po[0xa2e4800超级视图]超级视图]超级视图]超级视图]超级视图]
(id)$11=0x00000000
在这里,我发现从
isUserInteractionEnabled
到根目录(ui窗口
)的所有视图都返回YES
。窗口的超级视图为零。我有一个自定义按钮,它将UIControl
子类化,并将其放在UICollectionReusableView
中。为了使它工作,我使控件的每个子视图及其子视图的子视图不处理用户交互
func disableUserInteractionInView(view: UIView) {
view.userInteractionEnabled = false
for view in view.subviews {
self.disableUserInteractionInView(view)
}
}
// My control has a subview called contentView which serves as a container
// of all my custom button's subviews.
self.disableUserInteractionInView(self.contentView)
添加该代码后,
.TouchUpInside
中的所有事件侦听器都将启动,按下该控件时,控件将在视觉上高亮显示。@robmayhoff:问题在于,我将错误的父类划分为子类。谢谢你抓到这个@markdorison哈哈,很有趣。我真的没料到这会是个问题。@robmayhoff:我和你一样惊讶,当我做了一个改变并且成功了!