如何只在viewDidAppear中做一些东西? [英] How to do some stuff in viewDidAppear only once?

查看:102
本文介绍了如何只在viewDidAppear中做一些东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查粘贴板并在显示视图时显示警告(如果它包含特定值)。我可以将代码放入 viewDidLoad 以确保它只被调用一次,但问题是警报视图显示得太快。我知道我可以设置一个计时器来推迟警报的外观,但我认为这不是一个好的解决办法。

I want to check the pasteboard and show an alert if it contains specific values when the view appears. I can place the code into viewDidLoad to ensure it's only invoked once, but the problem is that the alert view shows too quickly. I know I can set a timer to defer the alert's appearance, but it's not a good work-around I think.

我检查了问题iOS 7 - viewDidLoad和viewDidAppear之间的区别,发现有一个步骤可以检查视图是否存在。所以我想知道是否有这样做的api?

I checked the question iOS 7 - Difference between viewDidLoad and viewDidAppear and found that there is one step for checking whether the view exists. So I wonder if there's any api for doing this?

更新:只有一次意味着视图控制器实例的生命周期。 / p>

Update: The "only once" means the lifetime of the view controller instance.

推荐答案

您可以使用标准的内置方法。

There is a standard, built-in method you can use for this.

Objective-C:

Objective-C:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([self isBeingPresented] || [self isMovingToParentViewController]) {
        // Perform an action that will only be done once
    }
}

Swift 3:

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

    if self.isBeingPresented || self.isMovingToParentViewController {
        // Perform an action that will only be done once
    }
}

当视图控制器首次显示为模态显示时,对 isBeingPresented 的调用为true。当视图控制器首次被推送到导航堆栈时, isMovingToParentViewController 为true。当视图控制器第一次出现时,其中一个将是真的。

The call to isBeingPresented is true when a view controller is first being shown as a result of being shown modally. isMovingToParentViewController is true when a view controller is first being pushed onto the navigation stack. One of the two will be true the first time the view controller appears.

无需处理 BOOL ivars或跟踪第一个电话的任何其他技巧。

No need to deal with BOOL ivars or any other trick to track the first call.

这篇关于如何只在viewDidAppear中做一些东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆