ios - UIButton 从不触发 Action 中的代码

标签 ios swift uibutton

我有以下屏幕:

enter image description here

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 中明确连接到操作:

enter image description here

当我检查 View 层次结构时,按钮顶部没有任何其他东西会阻止按钮,但会注册点击。我在这里假设 UIButton 中的 imageView 作为按钮本身的一部分是可点击的。

enter image description here

图中的X不是我加的image view,是UIButton自带的image view。话虽如此,我还采用了以下方法:

self.profilePageClosePopUpButton.imageView?.userInteractionEnabled = true;

按钮仍然无法点击。我哪里错了?

最佳答案

这么说让我很痛苦,但我只是在这里写一个解决方案,以防将来有人遇到同样的问题,也许这篇文章可以帮助他们。

在 View 中,如下图所示,我无意中关闭了界面构建器中启用的用户交互。因为这是关闭的,所以所有其他 child 都没有启用互动,因此按钮无法点击的原因......

enter image description here

故事的寓意是,检查您 parent 的观点并确保他们的用户交互也已启用。

关于ios - UIButton 从不触发 Action 中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317561/

相关文章:

ios - 在 NSOperation 中取消 NSData initWithContentsOfURL

ios - 如何以编程方式将 UIButton 添加到 UIToolBar?

ios - Game Center 无法识别游戏

ios - 如何设置重叠时哪个控件显示在顶部?

ios - navigationItem.TitleView 不适用于 iOS 10

ios - 使用图表设置饼图的最大值和宽度

ios - 为什么我们可以显式地解包这个可选的而不用担心崩溃?

arrays - 遍历数组只获取 Swift Playground 中的第一个值

ios - 保持 UIbuttons 背景图片的纵横比

ios - 静音播放背景歌曲但不显示静音图像?