2016-10-27 6 views
0

私のアプリケーションではUIViewController "ProfilVC"はUINavigationController "nav"の一部です。現在のUIViewControllerがUINavigationControllerの一部であるかどうかを確認する方法

しかし、別のシーンでは、私はUIViewController同じですが、異なるデータとを再利用する「ProfilVCを提示しています。 didSelectRowAt

let vc = tweetsStb.instantiateViewController(withIdentifier: "ProfilVC") as! ProfilVC 
self.present(vc, animated: true, completion: nil) 

、私は別のUIViewControllerを提示しています。そして、ここで私はselfProfilVC)が現在navの一部であるか、または上に提示されているかどうかを確認する必要があります。 ProfilVCnavの一部である場合

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let vc = tweetsStb.instantiateViewController(withIdentifier: "TweetCommentsVC") as! TweetCommentsVC 
    let post = tweets[(indexPath as NSIndexPath).section] 
    let cell = tableView.cellForRow(at: indexPath) as! TweetPostCell 
    vc.commentCounterString = cell.commentLabel.text! 
    vc.post = post 
    vc.avatare = avatare 
    nav?.present(vc, animated: true, completion: nil) 
} 

このコードは完璧に動作します。

ProfilVCが表示されている場合はself.present(vc, animated: true, completion: nil)が必要です。 ProfilVCは常にnavの一部であることを、

if(nav?.topViewController?.isKind(of: ProfilVC.self) != nil) { 
    nav?.present(vc, animated: true, completion: nil) 
} else { 
    self.present(vc, animated: true, completion: nil) 
} 

このコードに問題がある:私は必要なもの

はこのようなものです。私は自分の望むを達成するための最良の方法は何

... ProfilVC"self"として/「現在の見える1」はnavまたは上に提示の一部であるかどうかを確認する必要がありますか?ヘルプは非常に感謝しています。

PS:私はnavのこのcase to overlapp the UINavigationBarProfilVCを必要とするのでnavProfilVCの2番目のインスタンスを提示にはオプションではありません。

+0

少なくともコメントしてください? –

答えて

3

すべてのViewControllerにはnavigationController propertyがあります。 ViewControllerがナビゲーションスタックの一部でない場合、このプロパティはnilです。

+0

ご協力いただきありがとうございます –