我有以下屏幕:
X 是 UIButton
的图像,我已将适当的操作添加到按钮。然而,当我点击按钮时,它永远不会触发操作中的代码。
这是一些代码:
@IBAction func CloseProfilePage(sender: AnyObject) {
self.removeAnimate();
}
这是用于启动 View Controller 的代码:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedAppointment = self.dayDatasource!.appointmentAtIndex(indexPath.item);
let profilePageViewController = ProfilePageViewController.init(withAppointment: selectedAppointment);
profilePageViewController.view.frame = self.view.frame
self.view.addSubview(profilePageViewController.view)
profilePageViewController.didMoveToParentViewController(self)
}
按钮在 xib 中明确连接到操作:
当我检查 View 层次结构时,按钮顶部没有任何其他东西会阻止按钮,但会注册点击。我在这里假设 UIButton
中的 imageView 作为按钮本身的一部分是可点击的。
图中的X不是我加的image view,是UIButton
自带的image view。话虽如此,我还采用了以下方法:
self.profilePageClosePopUpButton.imageView?.userInteractionEnabled = true;
按钮仍然无法点击。我哪里错了?
最佳答案
这么说让我很痛苦,但我只是在这里写一个解决方案,以防将来有人遇到同样的问题,也许这篇文章可以帮助他们。
在 View 中,如下图所示,我无意中关闭了界面构建器中启用的用户交互。因为这是关闭的,所以所有其他 child 都没有启用互动,因此按钮无法点击的原因......
故事的寓意是,检查您 parent 的观点并确保他们的用户交互也已启用。
关于ios - UIButton 从不触发 Action 中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317561/