使用未解析的标识符' addChild'

时间:2015-10-12 18:53:10

标签: ios swift addchild

任何人都可以帮助我插入我的highscoreLabel,我一直在生成错误,我已经看过将代码移动到viewDidAppear和viewDidLoad。

我刚刚开始使用swift,所以如果有人能提供帮助那就太棒了。

谢谢。

import UIKit
import SpriteKit
import AVFoundation
import GameKit

class StatsViewController: UIViewController {

var highScoreLabelNode = SKLabelNode()
var highScore = NSInteger()
var highscoreDefault = NSInteger()

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

            let highscoreDefault = NSUserDefaults.standardUserDefaults()
    let score = highscoreDefault.valueForKey("HighScore")

    highScoreLabelNode.fontName = "Helvetica-Bold"
    highScoreLabelNode.position = CGPoint(x: 2.48, y: 1.17 )
    highScoreLabelNode.fontSize = 30
    highScoreLabelNode.alpha = 0.7
    highScoreLabelNode.text = "Highscore \(score)"

    addChild(highScoreLabelNode) //USE OF UNRESOLVED IDENTIFIER 'ADDCHILD'

}



override func viewDidLoad() {
    super.viewDidLoad()

        }

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientationMask.AllButUpsideDown
    } else {
        return UIInterfaceOrientationMask.All
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}



override func prefersStatusBarHidden() -> Bool {
    return true
}
} 

1 个答案:

答案 0 :(得分:1)

查看错误,您应该看到问题在于addChild不是函数,因此将其放在其他函数中将无法解决此问题。您需要在视图控制器的视图上调用正确的函数。

根据以下相关问题的答案,我认为您需要:

self.view.addSubview(highScoreLabelNode)

How to add child to view controller swift?