菜鸟教程小白 发表于 2022-12-12 20:35:30

ios - Objective-C:有哪些设计模式可以将模型与从属性列表中选择的 View 连接起来?


                                            <p><p>我正在尝试使用 Objective-C 构建 MVC 应用程序。它应该是一个问卷调查应用程序。我将所有问题和多项选择答案存储到属性列表中,因为我有不同的问卷,我希望能够使用这个应用程序加载。这个想法是主模型将跟踪它应该读取属性列表的哪个项目,并选择相应的 View 和 ViewController 。</p>

<p>所以示意性地我有以下问题。
<img src="/image/JpfSS.jpg" alt="enter image description here"/> </p>

<p><strong>RootView</strong> 显示开始菜单,用于选择您可以参加的问卷调查。</p>

<p><strong>RootViewController</strong> 是应用委托(delegate)调用的第一个 Controller 。它应该实例化模型并显示 RootView。它还控制 RootView 的按钮。</p>

<p><strong>模型</strong>应该将属性列表的项目包装成合适的数据结构,并将其提供给需要它的 ViewController 。 </p>

<p><strong>SelectedViewController</strong> 是一个 Controller ,它是专门为一类问题制作的模板。这个问题可以是多项选择,一个开放式问题,一个 3、5 或 7 选择的李克特量表类型的问题,任何东西。这些 ViewController 真正会得到的模板名称是 ViewController。</p>

<p><strong>SelectedView</strong> 是针对问题类型量身定制的 View ,将获得与所有选定 ViewController 相同的名称格式。</p>

<p>这是我的想法。</p>

<ul>
<li>我最初的预感是使用委托(delegate)模式,并将模型设置为任何 SelectedViewController 的委托(delegate)。</li>
<li>我还可以对 RootViewController 使用委托(delegate)模式,并让他监控 SelectedViewController 是否应该被销毁(通过委托(delegate)消息)。在这种情况下,我可以在 RootViewController 中为 SelectedViewController 实现 prepareForSegue。</li>
<li>由于它是来自 plist 的问卷,我还可以添加一个为 segue 准备的问卷
每个选定的 ViewController ,但这可能是一个问题,
因为至少有 15 种不同的显示方式
问题。</li>
</ul>

<p>根据 <a href="https://stackoverflow.com/questions/21010864/ios-cocoa-design-pattern-for-data-model-controller" rel="noreferrer noopener nofollow">to this question</a>,显然还有类似 Key-Value Observing 的东西。 .所以这也是我可以使用的东西。</p>

<p>我认为有一个明确的方法可以解决这个问题,因为 iOS 中的设计模式已经很好地描述了,所以应该有几个选项(或者只有一个)。目前我倾向于将 RootViewController 设置为 SelectedViewController 的委托(delegate),并让 RootViewController 处理模型。通过这种方式,我将 RootViewController 扩展为还包含每个 SelectedViewController 应具有的所有常见功能。</p>

<p>但我真的不确定这是否可行,因为我对设计模式的了解有限。我的问题是:在这种特定情况下(例如,通过 .plist 文件选择的 View 和 ViewController ),什么是正确的选择?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>不需要特定的模式 - 您可以处理通过名称访问模型对象的实例,即以与创建特定 View 和 ViewController 相同的方式。</p>

<p>假设您希望将 <code>QuizQuestionViewController4MC</code> 及其 <code>QuizQuestionView4MC</code> 连接到他们的模型。假设模型类名为 <code>QuizQuestionModel4MC</code>,并且需要使用您从 plist 中的键 <code>@"4MC"</code> 获得的对象进行配置。由于您的代码仅在运行时学习模型类的名称,因此您可以使用反射创建一个实例:</p>

<pre><code>NSDictionary *dataFromPlist = ... // Someone passes this to you
NSString *identifier = dataFromPlist[@&#34;identifier&#34;]; // Returns @&#34;4MC&#34;
NSString *ctrlName = ;
NSString *modelClassName = ;
id model = [ init];
// Configure the model with the data from plist
;
// The model is ready to be used - give it to the view controller
MyBaseViewController *ctrl = ;
// Give the view controller its model - now it is ready to be used
;
</code></pre>

<p>注意 ViewController 的类 - <code>MyBaseViewController</code>。这不是您的 Root ViewController ,它是您所有特定 ViewController 的基类。正是这个 ViewController 知道模型,但它不知道模型层次结构中的特定子类。 ViewController 的每个子类都知道其特定的模型子类,因此它可以直接从模型类中获取信息,而无需通过选择器或 KVP。</p>

<p>当然,由应用程序的设计者将正确的 ViewController “连接”到正确的模型。对于上面的例子,<code>QuizQuestionViewController4MC</code> 需要知道 <code>QuizQuestionModel4MC</code> 的结构,以避免将无法识别的选择器发送到错误的类。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Objective-C:有哪些设计模式可以将模型与从属性列表中选择的 View 连接起来?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22484562/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22484562/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Objective-C:有哪些设计模式可以将模型与从属性列表中选择的 View 连接起来?