gpt4 book ai didi

swift - 如何在 addtarget UIButton 中添加类方法?

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:39 26 4
gpt4 key购买 nike

我有两个类,其中一个我创建了一个按钮,我想在这个类中调用一个方法。

  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 实例)

关于swift - 如何在 addtarget UIButton 中添加类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com