Objective C与一个视图控制器进行通信,告知另一个视图控制器已更改 [英] Objective C Communicating to one view controller that another has changed

查看:51
本文介绍了Objective C与一个视图控制器进行通信,告知另一个视图控制器已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器-一个显示主要内容的主"视图控制器和一个滑出"视图中的容器中保存的设置"表视图控制器.这个概念是这样的:

I have two view controllers - one "main" view controller which displays the main content and one "settings" table view controller which is held in a container within a slide-out view. The concept is this:

  1. 用户在设置面板上点击一个项目.
  2. 设置TVC使用所有按钮设置创建一个按钮状态"字典对象.
  3. 按钮状态字典传递给一个类,该类将按钮状态转换为主要模型对象可以理解的设置字典.
  4. __ _ __ _ _ ???
  1. User taps an item on settings panel.
  2. Settings TVC creates a "Button States" dictionary object with all of the button settings.
  3. Button States dictionary is passed to a class that converts the button states to a dictionary of settings that the main model object can understand.
  4. _______???

这时,我需要通知主视图控制器(保存我的主模型对象的实例)设置已更新,并且需要更新其模型对象上的设置.我该怎么做呢?我应该在主视图控制器上使用类方法,并将头文件包含在我的设置转换类中吗?

At this point, I need to inform the main view controller (which holds the instance of my main model object) that the settings have been updated and it needs to update the settings on its model object. How do I go about doing this? Should I have a class method on the main view controller and include the header file in my settings conversion class?

推荐答案

您不需要将与模型的通信限制为仅主"视图控制器,您的设置表视图控制器也可以直接访问模型!将所有模型访问集中到一个地方通常不会给您带来很多好处,而是需要您发明无意义的并行系统来将模型值传递给&从那个主控制器.

You don't need to restrict the communication with your model to just the "main" view controller, your settings table view controller can directly access the model too! Funnelling all the model access through the one place doesn't usually get you much benefit, and instead demands you invent pointless parallel systems to communicate the model values to & from that main controller.

让模型对象成为单例对象,并允许任何控制器进行访问.

Make the model object(s) a singleton instead and allow access from any controller.

但是,如果您确实需要保留体系结构,则需要一种方法来发信号通知主视图控制器已关闭设置视图.也许发布主视图控制器观察到的通知,更新的设置字典可以是通知对象.

However if you really need to keep your architecture, you need a way of signalling back that the main view controller that the settings view is closing. Perhaps post a notification that the main view controller observes, the updated settings dictionary can be the notification object.

这篇关于Objective C与一个视图控制器进行通信,告知另一个视图控制器已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆