在swift中,无法获取UInavigationController的viewcontroller的索引。

问题描述 投票:1回答:1

我想检查导航控制器的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)"

ios swift uinavigationcontroller
1个回答
1
投票

index(of element: Element) -> Int? 已被废弃...所以使用 firstIndex(of element: Element) -> Int?

firstIndex(of:)

返回指定值出现在集合中的第一个索引。

 if let index =navigationController?.viewControllers.firstIndex(of: abcclass) {
     // do what you want to with index
    }
© www.soinside.com 2019 - 2024. All rights reserved.