不会调用为自定义按钮设置的目标操作

时间:2016-03-20 08:15:40

标签: ios swift uibutton selector target

我有这个UIViewController子类:

class MyViewController: UIViewController {

    let backgroundImageView: UIImageView = {
       let backgroundImage = UIImage(named:"backgroundImg")
       let backgroundImageView = UIImageView(image:backgroundImage)
       backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
       backgroundImageView.contentMode = .ScaleAspectFill
       return backgroundImageView
    }()

    let containerView: UIView = {
       let view = UIView()
       view.translatesAutoresizingMaskIntoConstraints = false
       view.backgroundColor = UIColor.whiteColor()
       return view
    }()

    let customButton: MyCustomButton = {
       let customButton = MyCustomButton()
       customButton.autoSetDimension(.Height, toSize: 50)
       return customButton
    }()

    // More properties and methods

    override func loadView() {
       self.view = UIView()
       self.view.backgroundColor = UIColor.whiteColor()

       self.view.addSubview(backgroundImageView)
       backgroundImageView.addSubview(containerView)

       self.view.addSubview(customButton)
       self.customButton.addTarget(self, action:"navigate", forControlEvents: UIControlEvents.TouchUpInside)

       view.setNeedsUpdateConstraints()
    }

    override func updateViewConstraints() {

       backgroundImageView.autoPinEdgesToSuperviewEdges()

       containerView.autoPinEdge(.Top, toEdge: .Bottom, ofView: (navigationController?.navigationBar)!, withOffset: 0)
       containerView.autoPinEdge(.Left, toEdge: .Left, ofView: backgroundImageView, withOffset: 0)
       containerView.autoPinEdge(.Right, toEdge: .Right, ofView: self.backgroundImageView, withOffset: 0)
       containerView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.backgroundImageView, withOffset: 0) code here

       self.customButton.autoPinEdge(.Left, toEdge: .Left, ofView: self.containerView, withOffset: 10)
       self.customButton.autoPinEdge(.Right, toEdge: .Right, ofView: self.containerView, withOffset: -10)
       self.customButton.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.containerView, withOffset: -10)

       super.updateViewConstraints()
   }

   func navigate() {
       let nextVC = NextViewController()
       self.navigationController?.pushViewController(nextVC, animated: true)
   }

}

我正在使用PureLayout框架来安排子视图。自定义按钮已正确显示,但当我点击它时,navigate方法未被调用,我不明白为什么......有人可以给我一个提示吗?

由于

编辑:在代码段中添加了视图层次结构。似乎自定义按钮没有以这种方式接收触摸事件,为什么?

0 个答案:

没有答案