【问题标题】:can not get index of viewcontroller which is viewcontroller of UInavigationController in swift无法快速获取视图控制器的索引,它是 UInavigationController 的视图控制器
【发布时间】:2020-08-29 23:07:59
【问题描述】:

我想检查导航控制器的 viewcontroller 是否包含 ABC Viewcontroller 是否包含想要弹出到 ABC 的前一个 viewcontroller 否则想要 popviewcontroller

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:) 在最近的 Swift 中,自 4.2 以来我想。,你的 Swift 版本是什么?在我的代码中,我收到警告:'index(of:)' is deprecated: renamed to 'firstIndex(of:)',以行 [3].index(of: 2) 为例
  • 我也在用swift 4.2版
  • 所以使用.firstIndex(of:) 而不是index(of:)
  • 是的,谢谢
  • 虽然类 abc 在 UINavigationController 中推送一次无法到达“if let index = self.navigationController?.viewControllers.indexOf(abcclass){”

标签: ios swift uinavigationcontroller


【解决方案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
    }

【讨论】:

  • 先生发生了什么事?你遇到的代码有什么问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
相关资源
最近更新 更多