从视图框架内编辑 UILabel

Edit UILabel From inside a View Frame

我在 ViewController 中的 Stack View 中有一个 UILabel 和一个 View,在同一个 [=15] 中我有一个 GameScene =].

'''''''''''''''  <- 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
}

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

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

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

在您的视图中,您在哪里设置对父级的引用(在您的情况下为 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
}