本文介绍了跳过快速的ViewController主要功能,例如viewDidDisappear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,当视图消失时,将发生特定操作.我正在通过 viewDidDisappear()函数进行此操作.我有一个特定的按钮,按下该按钮会转到另一个视图.我想知道如何仅告诉特定按钮跳过 viewDidDisappear()所引起的功能.

我完全知道我可以在 viewDidDisappear()中添加一种'if'语句,但是我想知道是否有一种更有效的方法.

解决方案

viewDidDisappear() UIViewController 的生​​命周期回调方法,到目前为止,环境已调用该方法据我所知,没有办法禁用其调用.而且我不认为应该存在-正如我提到的那样,它是 UIViewController 生命周期的一部分,不调用它会破坏合同-请参阅其

因此,您必须(并且应该)使用 if 语句实现所需的目标.

执行以下操作:

  fileprivate var skipDisappearingAnimation = false覆盖func viewDidDisappear(_动画:布尔){super.viewDidDisappear(动画)prepareInterfaceForDisappearing()}fileprivate func prepareInterfaceForDisappearing(){警卫!skipDisappearingAnimation else {//每次重置skipDisappearingAnimation = false返回}//做您通常需要的东西}@objc fileprivate func buttonPressed(_发件人:UIButton){skipDisappearingAnimation = true//向前导航} 

In my code, when a view disappears, a specific action occurs. I am doing it through the viewDidDisappear() function.I have a specific button that when is pressed it goes to another view. I was wondering in what I way I could tell ONLY the function caused by a specific button to skip the viewDidDisappear().

I perfectly know I can add a sort of 'if' statement in the viewDidDisappear() but I was wondering if there was a more efficient method.

解决方案

viewDidDisappear() is a UIViewController's lifecycle callback method that's called by the environment - as far as I know there is no way to disable its calling. And I don't think there should be - as I mentioned, it is a part of UIViewController's lifecycle, not calling it would break the contract - see its documentation.

Therefore you have to (and you should) achieve what you want by using if statement.

Do something like this:

fileprivate var skipDisappearingAnimation = false

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    prepareInterfaceForDisappearing()
}

fileprivate func prepareInterfaceForDisappearing() {
    guard !skipDisappearingAnimation else {
        // reset each time
        skipDisappearingAnimation = false
        return
    }

    // do the stuff you normally need
}

@objc fileprivate func buttonPressed(_ sender: UIButton) {
    skipDisappearingAnimation = true
    // navigate forward
}

这篇关于跳过快速的ViewController主要功能,例如viewDidDisappear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:46