我已经以编程方式创建了一个按钮,并希望添加一个触摸事件处理程序。我已经尝试了我在网上找到的所有技巧,但是从未触发过所需的功能。
upgradeNowView = UIButton()
upgradeNowView.setTitle("Upgrade", forState: .Normal)
upgradeNowView.contentMode = UIViewContentMode.ScaleToFill
upgradeNowView.frame = CGRect(x: upgradeNowView.frame.minX, y: -50 , width: 320, height: 50)
upgradeNowView.userInteractionEnabled = true
upgradeNowView.sendActionsForControlEvents(.TouchUpInside)
upgradeNowView.addTarget(self, action: #selector(MainViewContainer.upgradeTapped(_:)), forControlEvents: .TouchUpInside)
side_menu_controller?.view.addSubview(upgradeNowView)
并定义了这个
func upgradeTapped(sender: UIButton)
{
UpgradeViewController.openUpgrade()
}
但这永远不会被召唤。任何帮助将不胜感激。
答案 0 :(得分:0)
您需要在upgradeTapped
实例的类中定义side_menu_controller
函数。我猜,它是SideMenuController
类。
答案 1 :(得分:0)
我发现这是因为我将按钮放在视图旁边但在界限之外(仅在顶部):
upgradeNowView.frame = CGRect(x: upgradeNowView.frame.minX, y: -50 , width: 320, height: 50)
有两种解决方法: