菜鸟教程小白 发表于 2022-12-11 18:07:16

ios - UIBarButtonItem 取决于演示文稿。 swift 3


                                            <p><p>我遇到了一个问题,因为有两种方法可以显示我的 <code>ViewController</code>。</p>

<ol>
<li>第一种方法是 <code>performSegue(withIdentifier: "segue", sender: self)</code></li>
</ol>

<p>它工作得很好,因为我的 <code>navigationItem</code> 中有这个后退按钮:</p>

<p> <a href="/image/EdpEi.png" rel="noreferrer noopener nofollow"><img src="/image/EdpEi.png" alt="enter image description here"/></a> </p>

<ol 开始=“2”>
<li><p>然后我使用此代码呈现相同的 <code>ViewController</code>(来自另一个 <code>viewController</code>,而不是第一种情况):</p>

<pre><code>    let storyboard: UIStoryboard = UIStoryboard(name: &#34;Main&#34;, bundle: nil)
    let navVC = storyboard.instantiateViewController(withIdentifier: &#34;navViewController&#34;) as! UINavigationController
    let vc = navVC.topViewController as! ViewController            
    self.present(navVC, animated: true, completion: nil)
</code></pre> </li>
</ol>

<p>但是我的 <code>ViewController</code> 中没有任何后退按钮。</p>

<p><strong>我的问题是:</strong>当我使用这个功能时,我怎样才能保留我的<code>backButton(它是怎样的)</code>:<code>performSegue(withIdentifier: "segue", sender: self)</code> ,但是当我使用这个函数时添加按钮(可能看起来不同):<code>self.present(navVC, animated: true, completion: nil)</code></p>

<p><strong>注意:</strong> 在我的案例 1 中,我的 segue 直接连接到 <code>ViewController</code> ,但在案例 2 中,我展示了 <code>UINavigationController</code> 和 <code>ViewController </code> 是 <code>embed in</code> 在这个 <code>UINavigationController</code> 中的。</p>

<p><strong>编辑:</strong>我试过这段代码,但它总是打印:<code>"1........."</code>:</p>

<pre><code>if self.presentingViewController != nil {
            print(&#34;1..........&#34;)
      } else if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController{
            return print(&#34;2.........&#34;)
      } else if self.tabBarController?.presentingViewController is UITabBarController {
            return print(&#34;3........&#34;)
      }
</code></pre>

<p>而且这段代码也会打印:<code>"Else......."</code> :</p>

<pre><code>let isPresentingInAddMealMode = presentedViewController is UINavigationController

if isPresentingInAddMealMode {
    print(&#34;FirstOption......................&#34;)

} else {
    print(&#34;Else......................&#34;)

}
</code></pre>

<p>如果您需要更多信息,请告诉我。
非常感谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要检查presentedViewController 并添加返回按钮,如下所示。</p>

<pre><code>if () {
      // add your back button item here
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIBarButtonItem 取决于演示文稿。 swift 3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40763578/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40763578/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIBarButtonItem 取决于演示文稿。 swift 3