2017-04-19 5 views
0

次のビューをプッシュしていない:使用tabbarViewController.selectedViewContrellerは、私が使用したい

self.tabBarController?.selectedViewController?.navigationController?.pushViewController(vc, animated: false) 

が、これはスウィフトでは動作しませんでした。しかし、私が使用すると動作します。

[self.tabBarController.selectedViewController pushViewController:nextVC animated:NO] 

Objective-Cどうして?

+2

なぜあなたスウィフトコードはObjective-Cのコードと同じではないでしょうか? – rmaddy

答えて

0

を試してみてくださいtabBarController.selectedViewControllerがあなたのViewControllerの階層に基づいて正しい可能性があるUINavigationControllerインスタンスであると想定されます。

ただし、tabBarController.selectedViewControllerは、目的のCバージョンと一致しないUINavigationControllerインスタンスに埋め込まれていると想定しています。 navControllerにアクセスすると、ナビゲーションコントローラに(存在しない)親navigationControllerを要求しているので、nilを返します。

あなたの迅速な例は次のように固定することができますので、

guard let navigationController = tabBarController.selectedViewController as? UINavigationController else { 
    return 
} 

navigationController.pushViewController(vc, animated: false) 
+0

ありがとう!あなたの答えは正しい – lzqok

1

は、あなたのObjective Cの例では、この

tabBarController?.selectedView?.pushViewController(nextVC, animated: false) 
+0

ありがとうございましたが、うまくいきませんでした。 – lzqok

関連する問題