0

私はナビゲーションベースのアプリケーションをビルドしています。そこでは下部タブでナビゲートし、各タブには独自のナビゲーションスタックがあります。 部品のほとんどは期待通りに機能しますが、特定のタブが予期せず動作します。何が起こっているperformSegueは次のビューコントローラを不要なビュー階層にプッシュします

は次のとおりです。

いくつかのビューコントローラをナビゲートした後、のは言う[A] [B] [C]と[D]、次のコントローラ[E]が突然ナビゲーションスタック消灯しましょう。 ビューがモーダルに表示されているように見え、ナビゲーションスタックは次のようになります。

(lldb) po [[[UIWindow keyWindow] rootViewController] _printHierarchy] 

<MyApp.CRTabBarController 0x112026800>, state: disappeared, view: <UILayoutContainerView 0x111e1fa00> not in the window 
    | <UINavigationController 0x112027a00>, state: disappeared, view: <UILayoutContainerView 0x111e23f50> not in the window 
    | | <MyApp.X1_ViewController 0x111d12490>, state: disappeared, view: <UIView 0x111e32d70> not in the window 
    | | | <MyApp.InsideX1_ViewController 0x112096200>, state: disappeared, view: <UIView 0x111e35d90> not in the window 
    | <UINavigationController 0x11200aa00>, state: disappeared, view: (view not loaded) 
    | | <MyApp.X2_ViewController 0x111d22360>, state: disappeared, view: (view not loaded) 
    | <UINavigationController 0x11205a200>, state: disappeared, view: (view not loaded) 
    | | <MyApp.X3_Controller 0x11205a800>, state: disappeared, view: (view not loaded) 
    | <UINavigationController 0x112059600>, state: disappeared, view: (view not loaded) 
    | | <MyApp.X4_ViewController 0x111e0cea0>, state: disappeared, view: (view not loaded) 
    | <UINavigationController 0x112058e00>, state: disappeared, view: <UILayoutContainerView 0x111d50370> not in the window 
    | | <MyApp.A_ViewController 0x111d25f70>, state: disappeared, view: <UIView 0x116e0ad90> not in the window 
    | | | <MyApp.PageViewController 0x112891400>, state: disappeared, view: <_UIPageViewControllerContentView 0x113feb0a0> not in the window 
    | | | | <MyApp.InnerPageViewController 0x111ee3bf0>, state: disappeared, view: <UIView 0x111e6e000> not in the window 
    | | <MyApp.B_ViewController 0x113ee8280>, state: disappeared, view: <UIView 0x116e37cf0> not in the window 
    | | <MyApp.C_Controller 0x118f06f10>, state: disappeared, view: <UIView 0x113ee46f0> not in the window 
    | | <MyApp.D_ViewController 0x116e5f580>, state: disappeared, view: <UIView 0x118f2fe70> not in the window 
    + <E_ViewController 0x116ecff30>, state: appeared, view: <UIView 0x111ee43f0>, presented with: <_UIFullscreenPresentationController 0x116e13420> 

上から4つのナビゲーションコントローラは、ここでは関心のない非アクティブなタブです。最後のタブのナビゲーションには、A_ to D_ - ViewController'sが表示されます。次に、E_ViewControllerが異なるナビゲーション階層にあり、これがここでの問題です。

コードはかなりまっすぐです。D_ViewControllerはこのようにperformSegueを呼び出すだけです。

self.performSegue(withIdentifier: "goto_E", sender: self) 

Segueの設定もかなり普通だと思います。これはidと "Show"をスタイルとして指定するだけです。 SegueD_ViewControllerからE_ViewControllerに接続されているので、performSegue's識別子argと呼ばれることがあります。

enter image description here

は、誰もが経験のこの種を持っていましたか?考えられる理由を推測しますか?

+0

DからEへのセグは、チェーン内の他のものと同じですか?BからCまたはCからD? –

+0

はい、すべて同じです。私は視覚的にセグをクリックして比較し、識別子以外は何も変わらなかった。 –

+0

そして、B_ViewControllerはC_ViewControllerに、C_ViewControllerはD_ViewControllerに同じように移動しますか? –

答えて

0

//セグエを外し、

extension UIViewController { 

    class func instantiate(fromStoryboard name: String, id: String) -> Self? { 
     return instantiateHelper(fromStoryboard: name, id: id) 
    } 

    private class func instantiateHelper<T>(fromStoryboard name: String, id: String) -> T? { 
     let storyboard = UIStoryboard(name: name, bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: id) as? T 
     return controller 
    } 
} 

使用をインスタンス化: - コメント

guard let vc = ViewController.instantiate(fromStoryboard: "Main", id: "ViewController") else { return } 
self.navigationController?.pushViewController(vc, animated: true) 
+0

なぜですか? segueを削除してインスタンス化を使用する理由は何ですか?それはすべてのために行われなければならないし、セグは使われないだろうか? –

+0

良い回避策のように見えます。ポイントはストーリーボードからVCをつかんでナビゲーションコントローラに直接プッシュすることですね。私はそれを打つことを許可しなさい。 –

+0

@UpholderOfTruth多くの場合、segueは正しく動作しません。だから、インスタンス化する方が良いです。 –

0

おかげで、私は自分でそれを解決してきました。 これは恥ずかしいことですが、その理由はかなり愚かで、それはperformSegueを呼び出す前にpopViewControllerと呼んでいたのです。

このポップコールは、ビュー構造が変更されたことが原因であり、別の場所に書かれているため気付きにくいです。

直接的な回答ではありませんが、ストーリーボードから直接インスタンス化するアプローチは、問題を明確にするのに役立ちました。ありがとう、Pratyush。

しかし、セグの結果を引き出す前にビューコントローラをポップすると、この種の動作が発生することは全く予想外でした。 これが他の人の問題のヒントになることを願っています。

関連する問題