0

我在里面有一个UILabel和一个,我在那个里面有一个。ViewStack ViewViewControllerGameSceneView

'''''''''''''''  <- GameStartViewController
'      L      '
'  '''''''''  '     L = UILabel
'  '   V   '  '     V = View Frame
'  '       '  '
'  '       '  '
'  '       '  '
'  '''''''''  '
'             '
'''''''''''''''

我正在尝试更新视图内部的分数标签,但我不断得到

fatal error: unexpectedly found nil while unwrapping an Optional value

即使它已连接,我也无法更改颜色、设置文本等...

代码:

GameScene.swift

var gs: GameStartViewController!

override func didMoveToView(view: SKView) {
    physicsWorld.contactDelegate = self
    self.backgroundColor = UIColor.feelFreeToColor()
    gs.scoreLabel.text = "\(score)" // THIS DOES NOT WORK
}

GameStartViewController.swift

@IBOutlet weak var selfView: SKView!
@IBOutlet weak var scoreLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.feelFreeToColor()
    self.initAdMobBanner()

    let scene: GameScene = GameScene(size: view.bounds.size)
    let skView = selfView
    scene.scaleMode = .AspectFill
    skView.presentScene(scene)

    scoreLabel.layer.addBorder(UIRectEdge.Bottom, color: UIColor.blackColor(), thickness: 3) // THIS WORKS
}

我可以从视图控制器编辑它,但不能在视图框架内。如何从视图框架内访问它?

4

2 回答 2

0

如果你尝试会发生什么gs.scoreLabel.text = "Test content"

你在哪里设置和填充分数变量?

于 2016-05-11T06:37:17.203 回答
0

您在哪里设置对您视图中父级的引用(在您的情况下gs)?

尝试在 ViewController 中设置父引用,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.feelFreeToColor()
    self.initAdMobBanner()

    let scene: GameScene = GameScene(size: view.bounds.size)

    // Notice!
    scene.gs = self

    let skView = selfView
    scene.scaleMode = .AspectFill
    skView.presentScene(scene)

    scoreLabel.layer.addBorder(UIRectEdge.Bottom, color: UIColor.blackColor(), thickness: 3) // THIS WORKS
}
于 2016-05-11T06:41:14.383 回答