调用applicationDidBecomeActive时如何告诉活动视图控制器? [英] How to tell the active view controller when applicationDidBecomeActive is called?
问题描述
我觉得我在这里错过了一个技巧...
I feel I am missing a trick here...
我只想在调用applicationDidBecomeActive时调用当前活动视图控制器上的viewDidLoad或viewDidAppear,所以我当应用程序从后台再次启动时,可以重置某些动画或其他内容。我的一些观点并不关心,但其他人真的需要知道。
I just want to call viewDidLoad or viewDidAppear on the current active view controller when applicationDidBecomeActive gets called, so I can reset some animations or whatever, when the app is started up again from the background. Some of my views don't care, but others really need to know.
我使用的是故事板,而我的app委托文件具有标准功能 - 但所有的都有EMPTY机构。例如,didFinishLaunchingWithOptions只返回YES而不执行任何操作。故事板自动完成我猜的所有事情。
I am using Storyboards and my app delegate file has the standard functions - but all with EMPTY bodies. For example, didFinishLaunchingWithOptions just returns YES and does nothing else. Storyboard automagically does everything I guess.
那么如何从我相当空白,无信息的应用代表那里与当前视图控制器交谈?
So how can I talk to the current view controller from my rather blank, information free, app delegate?
推荐答案
我建议使用通知。
在你的应用委托中,applicationdidBecomeActive方法放在这段代码中:
In your app delegate's applicationdidBecomeActive method put in this code:
[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];
在当前活动视图控制器的init方法中订阅通知。
In your current active view controller's init method subscribe to the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateStuff)
name:@"appDidBecomeActive"
object:nil];
在你的控制器中实现updateStuff方法,你应该能够做任何你想做的事情。应用程序变为活动状态。
Implement the "updateStuff" method in your controller and you should be able to do whatever you want when the app becomes active.
这篇关于调用applicationDidBecomeActive时如何告诉活动视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!