I'm calling a method from my DrawMenu
class in my ViewController
class, which draws an oval (currently circle) button, pretty simple. It draws the button perfectly, but if I tap the button it crashes.
This happens even though I have created an instance of the ViewController
class in DrawMenu
, and have used it for the 'target' parameter in 'button.addTarget'
Here is the code:
Button Method defined in DrawMenu
class:
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)
}
}
Method called in ViewController
class:
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
also in ViewController
class:
func buttonTap(sender:UIButton!){
println("Button is working")
}
Any help is appreciated. Thank You