[英]How to call viewDidLoad from applicationDidBecomeActive
there are a few other answers for this question, but they doesn't help me so much.. 这个问题还有其他一些答案,但是它们对我没有太大帮助。
so i need a solution to run the viewDidLoad
method from the AppDelegate
especially from the applicationDidBecomeActive
method. 所以我需要一个解决方案来从
AppDelegate
尤其是从applicationDidBecomeActive
方法运行viewDidLoad
方法。 please help me. 请帮我。
Thanks in advance!! 提前致谢!!
viewDidLoad
is always called by the system, and you ideally shouldn't call it manually. viewDidLoad
总是由系统调用,理想情况下,您不应该手动调用它。 That said, one way to call it is to access the .view property of a UIViewController
. 就是说,一种调用它的方法是访问
UIViewController
的.view属性。 This does call viewDidLoad
, if it hasn't already been called. 如果尚未调用
viewDidLoad
,则会调用该方法。
If there is some piece of code that should run everytime a view appears, you should write your code in viewWillAppear
or viewDidAppear
如果每次视图出现时都应运行一些代码,则应在
viewWillAppear
或viewDidAppear
编写代码
From your comment it seems you want to call a function on the view controller when the app becomes active? 从您的评论看来,您想在应用程序激活时在视图控制器上调用一个函数吗?
You can add an observer to your view controller, if the class has an init method that is getting called do it there, if not do it in viewDidLoad: 您可以将观察者添加到您的视图控制器中,如果该类具有被调用的init方法,请在该方法中进行操作;如果未在viewDidLoad中进行操作,则可以执行以下操作:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appBecameActive)
name: UIApplicationDidBecomeActiveNotification
object:nil];
You need to remove it when the object is removed, so add this: 您需要在删除对象时将其删除,因此添加以下内容:
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Then the method appBecameActive will be called every time the app becomes active 然后,每次应用程序激活时,都会调用方法appBecameActive
- (void) appBecameActive
{
call your method here
}
PS From your comments + is for class methods. PS从您的评论+是用于类方法。 Which with a view controller is probably not what you want.
哪一个带有视图控制器可能不是您想要的。 Read about class methods versus object methods to continue your iOS education.
阅读有关类方法和对象方法的信息,以继续进行iOS学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.