EXC_BAD_ACCESS,从另一个类调用按钮方法时

时间:2015-09-11 04:13:01

标签: ios swift uibutton addtarget

我在我的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")
}

感谢任何帮助。 谢谢

0 个答案:

没有答案