gpt4 book ai didi

ios - 将目标添加到 super View 的 UIButton

转载 作者:行者123 更新时间:2023-11-30 13:59:26 25 4
gpt4 key购买 nike

我正在以编程方式将 UIView 添加到 SKScene。在此 UIView 中,我有一个按钮,应在 SKScene 中触发命令。我该怎么做呢?我的代码抛出异常。或者这可能只是不干净的编程?

class LevelAchievedView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)
self.opaque = false
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func drawRect(rect: CGRect) {
let replayButton = UIButton()
replayButton.setTitle("Replay?", forState: UIControlState.Normal)
replayButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
replayButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 30)
replayButton.addTarget(self.superview, action: "returnToStart", forControlEvents: .TouchUpInside)
replayButton.frame.origin = CGPointMake(10, 10)
self.addSubview(replayButton)
}

看来我搞砸了replayButton.addTarget(self.superview...,我想在这种情况下我不能使用self.superview,尽管我有func returnToStart() 在 SKScene 中。我缺少什么?

抛出的异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SKView returnToStart]: unrecognized selector sent to instance 0x7ff10980fc00'

最佳答案

self.superview 不是 SKScene。只是一个解决方案:

class LevelAchievedView: UIView {
let replayButton: UIButton

override init(frame: CGRect) {
self.replayButton = UIButton()

super.init(frame: frame)
self.opaque = false

replayButton.setTitle("Replay?", forState: UIControlState.Normal)
replayButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
replayButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 30)
replayButton.frame.origin = CGPointMake(10, 10)
self.addSubview(replayButton)
}
}

class MyScene: SKScene {
override func didMoveToView(view: SKView) {
// ...
let levelAchieved = LevelAchievedView(rect: CGRect(...))
levelAchieved.replayButton.addTarget(self, action: "returnToStart", forControlEvents: .TouchUpInside)
view.addSubview(levelAchieved)
}

func returnToStart() {
// your code here
}
}

关于ios - 将目标添加到 super View 的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33173738/

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