Action-Target不被称为Swift中的自定义UIControl

时间:2015-02-10 19:45:03

标签: ios swift

我目前在尝试在我正在创建的自定义UIControl上执行操作目标时遇到问题。我从来没有创建过UIControl,并希望从创建一个带有多个单选按钮的开始。唯一的问题是当我尝试这样做时,我无法在action-target调用中调用该函数。最初认为问题是我在我正在创建的自定义tableview单元格中使用UIControl但是当我将UIControl放在其他地方时它仍然不会触发该动作目标。

import UIKit

class RadioButtonScaleControl: UIControl {
var firstButton:UIButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
    // Drawing code
}
*/

override init(frame:CGRect) {
    firstButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    super.init(frame:frame)


    self.generateButtonLayout()
}

required init(coder aDecoder: NSCoder) {
    firstButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

    super.init(coder:aDecoder)


    //     fatalError("init(coder:) has not been implemented")
    self.generateButtonLayout()
}

  func generateButtonLayout() {
      firstButton.frame = CGRectMake(20,70,26,23)

    let radioButtonClicked = UIImage(named: "BtnRadioActive") as UIImage
    let radioButtonDefault = UIImage(named: "BtnRadioInactive") as UIImage

    firstButton.setImage(radioButtonDefault, forState: UIControlState.Normal)
    firstButton.setImage(radioButtonClicked, forState: UIControlState.Selected)


    firstButton.addTarget(self, action:"buttonClicked:", forControlEvents:.TouchUpInside)


}

func buttonClicked(sender:AnyObject)
{
    println("button clicked!")
    //sender.selected = !sender.selected;
}
}

所以上面我从未进入buttonClicked功能。我尝试点击按钮但没有采取任何行动。下面是我称之为的自定义tableview单元格   UIControl来自:

import UIKit

   class RadioButtonsCustomCell: UITableViewCell {
       var curRadio:RadioButtonScaleControl
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    curRadio = RadioButtonScaleControl(frame:CGRectZero)
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init(coder aDecoder: NSCoder) {
    //fatalError("init(coder:) has not been implemented")
    curRadio = RadioButtonScaleControl(frame:CGRectZero)
    super.init(coder: aDecoder)
}

override func awakeFromNib() {
    super.awakeFromNib()
    self.addSubview(curRadio)
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state

}

}

有没有人遇到过类似的问题?任何建议将不胜感激。感谢。

0 个答案:

没有答案