gpt4 book ai didi

ios - Swift 中的 UIButton - 操作不起作用

转载 作者:行者123 更新时间:2023-11-28 10:01:21 29 4
gpt4 key购买 nike

我正在编写我的第一个 IO 应用程序。我有我的类(class) animation,我在其中为我的按钮注册了一个 Action

class animation {
var view: UIView!
init(var viewLink:UIView) {
self.view = viewLink
let buttonNext = UIButton.buttonWithType(UIButtonType.System) as UIButton
buttonNext.frame = CGRectMake(200, 250, 100, 50)
buttonNext.addTarget(self, action: "nextActionButton:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buttonNext)
}
}
class ViewController: UIViewController {
private var animClass: animation?

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()
animClass = animation(viewLink: self.view)
}

func nextActionButton(sender:UIButton!) {
println('rrr')
}
}

我有一个错误:NSForwarding:警告:“animation.animation”类的对象 0x7fd820c94c00 未实现 methodSignatureForSelector:——麻烦在前
无法识别的选择器 -[animation.animation nextActionButton:]

你能帮我看看我做错了什么吗?

最佳答案

您将按钮的 Action 设置为以 animation 对象为目标,其中实际方法在您的 ViewController 中定义。

您应该将 ViewController 传递给动画的初始化类并将其用作目标:

init(var viewLink:UIView, eventTarget : AnyObject) {
...
buttonNext.addTarget(eventTarget, action: "nextActionButton:", forControlEvents: UIControlEvents.TouchUpInside)
...
}
...
override func viewDidLoad() {
...
animClass = animation(viewLink: self.view, eventTarget:self)
...
}

关于ios - Swift 中的 UIButton - 操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28321932/

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