UIButton in Swift is not registering touches

后端 未结 18 918
情深已故
情深已故 2020-12-29 21:11

I\'m trying to create a UIButton using Swift. It compiles fine and I can see my button in the simulator, but when I click it, nothing happens. This is the code I am using:

18条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 21:16

    I see it is an old question but I just faced very similar problem yesterday. My buttons were highlighted on touch but not firing the action.

    I have two view controllers. One view covers the others view and button is on the top view.

    eg.

    • Rootviewcontroller has back view

    • Topviewcontroller has top view

    The button on top view does not call the action if I don't add the topviewcontroler as childviewcontroller of the rootviewcontroller. After adding the topviewcontroller as childviewcontroller it started to working.

    So in short: just try to add the view controller of buttons superview as childviewcontroller to the parent views viewcontroller with the following method

    func addChildViewController(_ childController: UIViewController)
    

提交回复
热议问题