我在我的DrawMenu
课程中调用ViewController
课程中的方法,该课程绘制了一个椭圆形(当前为圆形)按钮,非常简单。它完美地绘制按钮,但如果我点击按钮就会崩溃。
即使我在ViewController
中创建了DrawMenu
类的实例,并将其用于'目标'参数' button.addTarget'
以下是代码:
DrawMenu
类中定义的按钮方法:
func drawButton (superImageView: UIImageView, x_of_origin: CGFloat, y_of_origin: CGFloat, width_of_oval: CGFloat, height_of_oval: CGFloat, actionSelector: Selector, want_to_test_bounds:Bool) {
var VC = ViewController()
var button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.addTarget(VC, action: actionSelector, forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRect(x: x_of_origin, y: y_of_origin, width: width_of_oval, height: height_of_oval)
button.clipsToBounds = true
button.layer.cornerRadius = height_of_oval/2.0
if (want_to_test_bounds == true) {
button.layer.borderColor = UIColor.blackColor().CGColor
button.layer.borderWidth = 1.0
superImageView.userInteractionEnabled = true
superImageView.addSubview(button)
} else {
superImageView.userInteractionEnabled = true
superImageView.addSubview(button)
}
}
ViewController
类中调用的方法:
override func viewDidLoad() {
super.viewDidLoad()
var drawMenu = DrawMenu()
drawMenu.drawButton(imageView, x_of_origin: 100, y_of_origin: 150, width_of_oval: 100, height_of_oval: 100, actionSelector: "buttonTap:" as Selector, want_to_test_bounds: true)
}
buttonTap
也位于ViewController
类:
func buttonTap(sender:UIButton!){
println("Button is working")
}
感谢任何帮助。 谢谢