Ios 无法获取viewcontroller的索引,它是swift中UInavigationController的viewcontroller

Ios 无法获取viewcontroller的索引,它是swift中UInavigationController的viewcontroller,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,我想检查导航控制器的viewcontroller是否包含ABC viewcontroller,如果它包含要弹出到ABC的上一个viewcontroller,否则要弹出viewcontroller class Common:UIViewController{ class func returnclassviewcontroller(storybordname:String,classname:String)->UIViewController{ let sb = UI

我想检查导航控制器的viewcontroller是否包含ABC viewcontroller,如果它包含要弹出到ABC的上一个viewcontroller,否则要弹出viewcontroller

class Common:UIViewController{
    class func returnclassviewcontroller(storybordname:String,classname:String)->UIViewController{
        let sb = UIStoryboard.init(name: storybordname, bundle: nil)
        var viewcontroller:UIViewController!
        viewcontroller = sb.instantiateViewController(withIdentifier: classname)
        return viewcontroller

      }
    }
    if let abcclass = Common.returnclassviewcontroller(storybordname: Constant.StoryboardNameLead, 
              classname: "ABC") as? ABC{

            if(self.navigationController?.viewControllers.contains(abcclass)?? false){
            if let index = self.navigationController?.viewControllers.indexOf(abcclass){

    self.navigationController?.popToViewController(self.navigationController?.viewControllers[index-1] ?? 
    self, animated: true)
    }else{
                              self.navigationController?.popViewController(animated: true)                                           
     }
                            }
                        }else{
                    self.navigationController?.popViewController(animated: true)
                        }
``
I am reaching to else part everytime , Though ABC Class is in naviagtion constorller's subview controller never reach to "self.navigationController?.popToViewController(self.navigationController?.viewControllers[index-1] ?? 
    self, animated: true)"

不赞成。。所以使用

firstIndex(of:)

返回指定值出现在索引中的第一个索引 收藏


.firstIndex(of:)
在最近的Swift中,从4.2开始,我想,你的Swift版本是什么?在我的代码中,我得到了警告:
“index(of:)”被弃用:重命名为“firstIndex(of:)”
,例如第
[3]行。index(of:2)
我也在使用swift版本4.2来使用
。firstIndex(of:)
而不是
index(of:)
是的,它的工作感谢,尽管在UINavigationController中推过一次的abc类无法到达“如果让index=self.navigationController?.viewControllers.indexOf(abcclass){”先生,发生了什么事?您面临的代码中有什么问题吗?
 if let index =navigationController?.viewControllers.firstIndex(of: abcclass) {
     // do what you want to with index
    }