ios - 跨多个 Controller 更新对象/模型
<p><p>我是 swift 的初学者。所以在我的项目中,我使用了多个对象/模型,我几乎在所有 Controller 中都使用过。我的问题是,当我的对象/模型在任何一个 Controller 中更新时,如何自动更新我的对象/模型(跨所有 Controller )?</p>
<p>执行此操作的正确方法是什么?我该怎么做??</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>有<code>Pass By Value & Pass By Reference</code>的概念。</p>
<p>最适合您的问题的解决方案是使用 <code>Pass By Reference</code> 类型。</p>
<p>如果我们谈论 <code>Swift 编程语言</code>。 </p>
<p><code>class</code> 是引用类型,<code>struct</code> 是值类型。</p>
<p>因此,您的模型类应该使用 <code>class</code> 类型构建。</p>
<p>代码示例。</p>
<pre><code>class Dog {
var breed : String = ""
var sub_breed = ()
init(_breed:String,_sub_breed:) {
self.breed = _breed
self.sub_breed = _sub_breed
}
}
FirstViewController:
var dogs : = )]
SecondViewController: // You are passing reference of dogs to SecondVC
var dogs : = []
dogs.breed = “Modified”
</code></pre>
<p>现在您已经从 <code>SecondViewController</code> 更改了 <code>breed name</code> 的值,如果您返回 <code>FirstViewController</code> 并检查 <code>first element</code> 数组的值将是 <code>“修改”</code></p>
<p>你可以试试这个想法。</p>
<p>其他解决方案:<code>Model 类</code>的<code>Singleton object</code>。</p>
<p>谢谢。</p></p>
<p style="font-size: 20px;">关于ios - 跨多个 Controller 更新对象/模型,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48021248/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48021248/
</a>
</p>
页:
[1]