为 superview 添加目标到 UIButton
Adding target to UIButton for superview
我正在以编程方式将 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,尽管我在 SKScene 中有 func returnToStart()
。我错过了什么?
抛出的异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SKView returnToStart]: unrecognized selector sent to instance 0x7ff10980fc00'
您需要实施 returnToStart。尝试添加如下内容:
function returnToStart() {
println("returning to start");
}
给你的LevelAchevedView
class
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
}
}
我正在以编程方式将 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,尽管我在 SKScene 中有 func returnToStart()
。我错过了什么?
抛出的异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SKView returnToStart]: unrecognized selector sent to instance 0x7ff10980fc00'
您需要实施 returnToStart。尝试添加如下内容:
function returnToStart() {
println("returning to start");
}
给你的LevelAchevedView
class
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
}
}