【问题标题】:Get back parentViewController取回父视图控制器
【发布时间】:2017-01-24 09:08:47
【问题描述】:

我有一个PhoneViewController : UIViewController

let phonePage = storyboard.instantiateViewControllerWithIdentifier("phoneViewController") as! PhoneViewController

我从 2 个不同的控制器中展示它。

// UIViewController1
self.navigationController?.pushViewController(phonePage, animated: true)

// UIViewController2
self.presentViewController(phonePage.embedInNavController(), animated: true, completion: nil)

我希望必须检测哪个控制器是其父级。我怎么能做到这一点?

【问题讨论】:

标签: swift uiviewcontroller swift2


【解决方案1】:

你可以试试这个代码:-

if let wd = self.window {
       var vc = wd.rootViewController
       if(vc is UINavigationController){
            vc = (vc as UINavigationController).visibleViewController
        }

        if(vc is YourViewController){
            //your code
        }
    }

【讨论】:

    【解决方案2】:

    简短的回答是:

    if let navController = self.navigationController {
        return navController.viewControllers[navController.viewControllers.count - 1]
        // take care if count <= 1
    else {
        return self.parent
    }
    

    但这真的是你想要的吗?你想根据谁是他的父母来实施什么行为? 我没有这个问题的答案,但你应该让你的代码可读。让我通过一个例子来解释:

    let phonePage = storyboard.instantiateViewControllerWithIdentifier("phoneViewController") as! PhoneViewController
    
    // Option 1
    self.navigationController?.pushViewController(phonePage, animated: true)
    phonePage.mode = PhonePageMode.list
    
    
    // Option 2
    self.presentViewController(phonePage.embedInNavController(), animated: true, completion: nil)
    phonePage.mode = PhonePageMode.grid
    
    // In your PhoneViewController class
    switch self.mode {
        case .list: // present as a list
        case .grid: // present as a grid
    }
    

    比 :

    更易读
    let phonePage = storyboard.instantiateViewControllerWithIdentifier("phoneViewController") as! PhoneViewController
    
    // Option 1
    self.navigationController?.pushViewController(phonePage, animated: true)
    
    // Option 2
    self.presentViewController(phonePage.embedInNavController(), animated: true, completion: nil)
    
    // In your PhoneViewController class
    guard let parent = self.parentViewController else { return }
    if parent is ThisClassWhichWantsAList {
        // present as list
    } else if parent is ThisOtherClassWhichWantsAGrid {
        // present as grid
    }
    

    如果您想使用其父项作为条件来做不同的事情,您宁愿使用附加属性。你未来的自己会心存感激的。

    【讨论】:

    • swift 3中没有parentViewController,只有parent。如果第二个VC被推送,父级将是navigationController。
    • 我最终在 childVC 上创建了一个属性,parentVC 在推送 childVC 时将设置该属性。不确定这是否是一个干净的修复
    • 如果您只需要知道谁是它的父级,这是完美的解决方案。如果您想操作父对象或在其上调用某些函数,那么delegation 就是您要寻找的模式
    【解决方案3】:

    如果你的视图控制器是从 navigationViewController 推送的,而不是 parentViewController 中可用的父类,并且如果呈现而不是可用的presentationController。

    你可以这样查看:

    if let parentVC = self.navigationController.parentViewController{
       //parentVC.className
    
    }
    
    if let presentedVC = self.navigationController?.presentationController{
       //presentedVC.className      
    }
    

    【讨论】:

      【解决方案4】:

      所以您基本上必须检查导航控制器堆栈中的视图控制器是否是您正在寻找的视图控制器类型,位于count - 1 的索引处。

      短版:

      if navigationController.viewControllers[navigationController.viewControllers.count - 1].isKind(of: TheVCYouAreLooking for.self) {
          print("it is")
      } else {
          print("it is NOT")
      }
      

      游乐场的长版:

      //These are your two view controllers
      class FirstVCClass: UIViewController {}
      class SecondVCClass: UIViewController {}
      
      
      let vc = FirstVCClass()
      let secondVC = SecondVCClass()
      //Create a navigationcontroller and add the first VC in the stack
      let navigationController = UINavigationController()
      navigationController.setViewControllers([vc], animated: true)
      
      //Now push the secondVC
      vc.navigationController?.pushViewController(secondVC, animated: true)
      
      //The last vc in the stack is the one you've just pushed
      print(navigationController.viewControllers.last!)
      
      // Now check
      if navigationController.viewControllers[navigationController.viewControllers.count - 1].isKind(of: FirstVCClass.self) {
          //The view controller that is before you current vc in the stack is of the class 
          print("it is")
      } else {
          print("it is NOT")
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-20
        • 1970-01-01
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 2015-07-14
        • 2011-06-30
        相关资源
        最近更新 更多