如何只在viewDidAppear中做一些东西?

编程入门 行业动态 更新时间:2024-10-26 21:29:57
本文介绍了如何只在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中做一些东西?

本文发布于:2023-05-27 19:33:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/300566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只在   中做   东西   viewDidAppear

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!