Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift viewController不更新_Swift_Uiviewcontroller_Viewdidload_Viewwillappear - Fatal编程技术网

Swift viewController不更新

Swift viewController不更新,swift,uiviewcontroller,viewdidload,viewwillappear,Swift,Uiviewcontroller,Viewdidload,Viewwillappear,我有2个UIViewController,一个启动游戏场景,另一个显示最高分,如下所示。问题是,我的第二个UIViewController不想显示udpated highscore。它只是一直显示0。 有人知道问题出在哪里吗?我试着使用viewDidLoad和ViewWillDisplay,但都不起作用 override func viewDidLoad() { super.viewDidLoad() let label = UILabel(frame: CGR

我有2个UIViewController,一个启动游戏场景,另一个显示最高分,如下所示。问题是,我的第二个UIViewController不想显示udpated highscore。它只是一直显示0。 有人知道问题出在哪里吗?我试着使用viewDidLoad和ViewWillDisplay,但都不起作用

override func viewDidLoad() {
        super.viewDidLoad()

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        label.center = CGPoint(x: 160, y: 285)
        label.textAlignment = .center
        label.text = String(GameScene().highScore)
        self.view.addSubview(label)

    }
在游戏场景中,我宣布最高分数如下:

var highScore = Int()
并在gameOver函数中使用,如下所示:

if score > highScore {highScore = score}
谢谢

label.text=String(GameSecene().highScore)

这条线表明GameSecene是一个静态类。否则,您就不能以这种方式访问变量

需要在视图控制器之间传递数据,才能在场景之间传递数据。您可以使用以下三个选项之一传递数据:

  • 虽然是performsgue()
  • PerformsgueWithIdentifier()并在IB中建立连接
  • 在IB中建立连接并定义操作

  • 您需要将数据从当前控制器传递到导航到的控制器。谢谢,这很有帮助,但现在我遇到了如何将高分值从游戏场景传递到视图控制器的问题,然后在另一个视图控制器中,我给了我相同的分数0,这是不正确的。什么是GameSecene.swift?它是扩展UIViewController的类还是扩展NSObject的类?它扩展了UIViewControllerThen您真的不应该在屏幕之间传递值时遇到问题。但这实际上似乎是你一直面临的问题。您可能需要重新考虑您的设计。变量传递得太多了。你可以使用UserDefaults将其作为应用程序级变量保存。好的,在其他方法多次失败后,我最终决定使用UserDefaults+user defaults保存高分,这正是我需要的。有趣的是,在ViewController之间传递变量时,变量不会刷新。谢谢。