菜鸟教程小白 发表于 2022-12-11 19:11:00

ios - 如何为多个屏幕的进程重用 View Controller ?


                                            <p><p>我正在开发一个聊天应用程序,它会提出一些问题以及用户可以在哪里回答。 </p>

<p>每个问题都有自己的屏幕,用户回答问题后,会有一个按钮导航到下一个屏幕。 </p>

<p>所有的屏幕几乎都一样。一个 UILabel 和输入字段。现在我想知道是否有办法重用这个 View ,所以我不必创建多个完全相同的 ViewController 。</p>

<p>这可能吗?我应该寻找什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以通过以下方式执行此操作:</p>

<ol>
<li><p>在重用 ViewController 中创建变量:</p>

<pre><code>var question: String!
</code></pre> </li>
<li><p>按钮 Action 需要执行segue:</p>

<pre><code>@IBAction func nextQuestionAction() {
    performSegue(withIdentifier: &#34;NextQuestionSegue&#34;, sender: nil)
}
</code></pre> </li>
<li><p>在为转场做准备时,在过渡之前,您分配下一个问题:</p>

<pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == &#34;NextQuestionSegue&#34; {
      guard let destinationVC = segue.destination as? ReuseViewController else { return }
      destinationVC.question = &#34;Question goes here&#34;
    }
}
</code></pre> </li>
</ol>

<p>这就是它的工作方式。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何为多个屏幕的进程重用 ViewController ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47071027/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47071027/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何为多个屏幕的进程重用 View Controller ?