【问题标题】:UIButton not responding to touchesUIButton 不响应触摸
【发布时间】:2016-12-14 23:09:41
【问题描述】:

我有以下看法:

它的构建方式

UIViewController --> UIView(底部方块)--> UIViewController(加载在底部方块中)

此 UIViewController 中的按钮按下不会被触发。但是,当我在模拟器中按下时,我确实看到了按钮动画。

此外,当我以编程方式将以下点击处理程序添加到此 UIViewController 中的圆形 UIImageView 时,它会响应:

func handleTap(sender: UITapGestureRecognizer? = nil) {
        print("tappable")
    }

let tap = UITapGestureRecognizer(target: self, action: #selector(NeighbourhoodViewController.handleTap(_:)))
userPopupView.userImageView.userInteractionEnabled = true
userPopupView.userImageView.addGestureRecognizer(tap)

如何捕捉对这些按钮的操作?这不起作用:

@IBAction func test(sender: AnyObject) {
        print("test")
    }

顺便说一句,该线连接到界面生成器中的 UIButton:

【问题讨论】:

  • 在相关按钮或视图上启用了用户交互?
  • 是的,用户交互无处不在。在 UIView 上。在 UIViewController 上。在 UIButton 上。按钮还会显示动画(按下时突出显示等),表示接收到触摸。
  • 这个“测试”方法是唯一链接到按钮插座的东西吗?查看 IB 中的 outlets 选项卡,确认没有冲突。
  • 添加了另一张图片。
  • 我一无所获...有兴趣知道这个问题的解决方案。

标签: ios iphone swift uiviewcontroller uibutton


【解决方案1】:

我找到了解决方案。我只是将 ViewController 添加到视图中:

self.popupView.addSubview(userPopupView.view)

应该是什么时候:

self.addChildViewController(userPopupView)
self.popupView.addSubview(userPopupView.view)
userPopupView.didMoveToParentViewController(self)

现在可以了!

【讨论】:

  • 还是不对。舞蹈是:addChildViewControlleraddSubview; didMoveToParentViewController - 按此顺序。做到这一点,别无其他。对此有严格的规定。
  • 谢谢马特!我已经调整了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2019-03-30
  • 2016-01-06
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
相关资源
最近更新 更多