如何检查当前UIViewController是否是UINavigationController的一部分

时间:2016-10-27 20:25:19

标签: ios swift

在我的应用中,UIViewControllerProfilVC”是UINavigationControllernav”的一部分。

但是在另一个场景中,我呈现ProfilVC”以重复使用相同的UIViewController,但使用不同的数据。

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

didSelectRowAt内,我正在展示另一个UIViewController。在这里,我需要检查selfProfilVC)当前是nav的一部分还是最重要的。

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属于nav,则此代码非常有用。

但如果self.present(vc, animated: true, completion: nil)出现在最顶层,我需要ProfilVC

我需要的是这样的事情:

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

此代码存在的问题是ProfilVC始终是nav的一部分。我需要检查ProfilVC "self" /“当前可见的”是nav的一部分还是最顶层显示...

实现我想要的最佳方法是什么?非常感谢帮助。

PS:在ProfilVC中提出nav的第二个实例是不可取的,因为我ProfilVC nav $studentID = "1234567"; if (preg_match('/^[0-9]{7,10}$/', $studentID)) { echo "$studentID matched!\n"; } $familyname = "Pupkin"; if (preg_match ('/^[A-Z -]{0,20}$/i', $familyname)) { echo "$familyname matched!"; } 需要{0,20}

1 个答案:

答案 0 :(得分:5)

每个ViewController都有一个navigationController property。如果ViewController不是导航堆栈的一部分,则此属性为nil。

相关问题