如何在 addtarget UIButton 中添加 class 方法?

How to add a class method in addtarget UIButton?

我有两个 classes,其中一个我创建了一个按钮,我想在这个 class.

中调用一个方法
  class blueFooterForImages: UIView{
    //buttons for footer
       let slideButton = UIButton()
       let buttonOnFooter = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)

        //setting buttons
        slideButton.frame = CGRectMake((self.frame.width / 2) - 10 , 10, 30, 30)
        slideButton.addTarget(self, action: "animationFunction:", forControlEvents: UIControlEvents.TouchUpInside)

        slideButton.setImage(imageForSlideButton,forState: .Normal)
        self.addSubview(slideButton)}
 }

internal func animationFunction(sender: UIButton!) {
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.frame.size.width -= 100
        })
    }

将此添加到您的代码中,使用单例并发送实例

static var sharedInstance : blueFooterForImages?

override init(frame: CGRect) {
    super.init(frame: frame)
    blueFooterForImages.sharedInstance = self
   //rest of init code....
} 

其他 class:

var button = UIButton(initParams...);
button.addTarget(blueFooterForImages.sharedInstance!,action:"animationFunction:", forControlEvents: UIControlEvents.TouchUpInside);

只需确保 blueFooterForImages.sharedInstance 不是 nil

(确保在执行此操作之前创建了 blueFooterForImages 实例)