【问题标题】:Detect when view controller appears from pop检测视图控制器何时从弹出窗口出现
【发布时间】:2019-06-09 22:53:57
【问题描述】:

我在导航堆栈深处弹出一个视图控制器。是否可以检测视图控制器是从推送还是弹出显示?

nav stack:

[A] -> [B] -> [C] -> [D] -> [E]

[E] 弹出到 [B]

nav stack:

[A]  -> [B] // Possible to detect if B appears from a pop?

【问题讨论】:

    标签: ios cocoa-touch uiviewcontroller uinavigationcontroller


    【解决方案1】:

    在视图控制器 B 中,实现 viewWillAppearviewDidAppear。在那里,使用isMovingToParentisBeingPresented 来查看它在什么条件下出现:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if !isBeingPresented && !isMovingToParent {
            // this view controller is becoming visible because something that was covering it has been dismissed or popped
        }
    }
    

    以下是人们可能会觉得方便的这些属性的更一般用法:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if isMovingToParent {
            // this view controller is becoming visible because it was just push onto a navigation controller or some other container view controller
        } else if isBeingPresented {
            // this view controller is becoming visible because it is being presented from another view controller
        } else if view.window == nil {
            // this view controller is becoming visible for the first time as the window's root view controller
        } else {
            // this view controller is becoming visible because something that was covering it has been dismissed or popped
        }
    }
    

    【讨论】:

    • 谢谢!我有一个问题.. isMovingToParent = true 当它被推送到导航堆栈时,因为它正在“移动到导航堆栈”?
    • @Mocha 没错。当您将 B 推到 A 上时,isMovingToParent 将变为 true
    • 一个疑问:在这种情况下不是isBeingPresented 总是false 吗?我的意思是,在这种情况下可以省略它,对吗?...或者如果我错了,什么时候可能是这个变量true
    • @ÁngelTéllez 是的,在这种特定情况下,它应该始终为 false 并被省略。我为了完成而展示。
    • 根标签栏控制器也不是这种情况。即使您切换选项卡,条件也会为真。
    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多