【问题标题】:Detect when ViewController has been presented by "popping" the previous VIewController in UINavigationController [duplicate]通过在 UINavigationController 中“弹出”前一个 VIewController 来检测何时呈现 ViewController [重复]
【发布时间】:2019-09-18 03:51:19
【问题描述】:

考虑以下导航层次结构:

NavigationContrller -> ViewController1 -> ViewController2

我想检测 ViewController1 何时从 ViewController2 出现,即按下 NavigationController 上的“返回”按钮。

我感兴趣的方法是- (void)viewWillAppear:(BOOL)animated。如何检查 ViewController1 是通过前进(即 NavigationController -> ViewController1)还是后退(即 ViewController2 -> ViewController1)?

【问题讨论】:

标签: ios objective-c uiviewcontroller uinavigationcontroller uikit


【解决方案1】:

您可以像这样在 viewWillAppear 中检查导航堆栈的控制器顺序:

      for controller in self.navigationController!.viewControllers as Array {
                //  print("controller \(controller) at i \(i)") 
      }

【讨论】:

    【解决方案2】:

    您可以使用Bool 来跟踪“推送状态”:

    class ViewController1: UIViewController {
    
        private var isBeingPushed = true
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            if isBeingPushed {
                print("forwards")
            } else {
                print("backwards")
            }
        }
    
        override func viewDidDisappear(_ animated: Bool) {
            isBeingPushed = false
            super.viewDidDisappear(animated)
        }
    
        override func didMove(toParent parent: UIViewController?) {
            if parent == nil {
                isBeingPushed = true
            }
            super.didMove(toParent: parent)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 2020-03-25
      • 2018-05-23
      • 2023-03-23
      相关资源
      最近更新 更多