简体   繁体   English

如何在多个情节提要视图之间共享模型?

[英]How can I share a model between multiple storyboard views?

The situation: 情况:

I'm writing my first iPhone app; 我正在写我的第一个iPhone应用程序。 it's an opengl game and I want to use separate views... The first view is the menu screen, a normal UIView/UIViewController with a high scores list and some buttons for configuring and starting a new game. 这是一个opengl游戏,我想使用单独的视图...第一个视图是菜单屏幕,带有高分列表的普通UIView / UIViewController和一些用于配置和启动新游戏的按钮。 The second view is a GLKView/GLKViewController combo, which will display the game. 第二个视图是GLKView / GLKViewController组合,它将显示游戏。

I have a model, which includes the game state, player data, the high scores list, etc. I need to share this model between the two views. 我有一个模型,其中包括游戏状态,玩家数据,高分列表等。我需要在两个视图之间共享该模型。 I've seen examples of this before, where the model is instantiated in the application delegate, and references to it are passed to each view controller that needs access to it. 我之前已经看过这样的示例,其中在应用程序委托中实例化了模型,并将对模型的引用传递给需要访问它的每个视图控制器。 I'd do it that way, but I don't know how to access the view controllers in my storyboard. 我会那样做,但是我不知道如何访问情节提要中的视图控制器。

My questions: 我的问题:

Is this the way I should be doing things? 这是我应该做的事情吗? To reiterate, I want my model to be owned by the AppDelegate, and I will pass references to the view controllers from the application:didFinishLaunchingWithOptions: method. 重申一下,我希望我的模型归AppDelegate所有,我将通过application:didFinishLaunchingWithOptions:方法将引用传递给视图控制器。 If there's a better way to solve this problem, please let me know. 如果有解决此问题的更好方法,请告诉我。

Assuming the above is the right way to do things, how would I go about doing it? 假设以上是正确的做事方式,我将如何去做? I'm not sure how to access the items in the storyboard. 我不确定如何访问情节提要中的项目。

Since the app delegate has references to the view controllers as well as the model, it should be able to update whichever view is currently active whenever the model changes. 由于应用程序委托引用了视图控制器以及模型,因此只要模型发生更改,它就应该能够更新当前处于活动状态的任何视图。

Ideally, the model shouldn't be talking directly to the views: it should tell the app delegate that it has changed, and then the app delegate will tell the appropriate view to update. 理想情况下,模型不应该直接与视图对话:它应该告诉应用程序委托它已更改,然后应用程序委托将告诉适当的视图进行更新。

I think this is the question you're asking; 认为这是您要提出的问题; if it's not, let me know what I'm missing and I'll try answering again. 如果不是,请让我知道我所缺少的内容,我将尝试再次回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM