简体   繁体   English

如何从applicationDidBecomeActive调用viewDidLoad

[英]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 如果每次视图出现时都应运行一些代码,则应在viewWillAppearviewDidAppear编写代码

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.

相关问题 使用 applicationDidBecomeActive 调用 viewDidLoad - use applicationDidBecomeActive to call viewDidLoad 在iOS上,如何调用applicationDidBecomeActive,loadView和viewDidLoad? - On iOS, how do applicationDidBecomeActive, loadView, and viewDidLoad get called? 如何从viewDidLoad调用“ didUpdateLocation”方法? - How to call “didUpdateLocation” method from viewDidLoad? 我如何从viewdidload调用按钮的方法 - how i call method of button from viewdidload 如何不在 uinavigationcontroller 中调用 viewdidload? - how to not call viewdidload in uinavigationcontroller? 如果是从通知或跳板触发的,如何确定applicationDidBecomeActive? - How to determine in applicationDidBecomeActive if triggered from a notification or springboard? 在“applicationDidBecomeActive”中调用“registerForRemoteNotificationTypes” - call “registerForRemoteNotificationTypes” in “applicationDidBecomeActive” 如何在iOS上的静态库中调用UIViewController :: viewDidLoad? - How to call UIViewController::viewDidLoad from inside a static library on iOS? 如何在iOS中按功能从同一个类调用ViewDidLoad方法 - How to call ViewDidLoad method from same class by function in iOS 如何在一个方法中Reaload viewDidLoad,不再调用诸如[self viewDidLoad]; - how to Reaload viewDidLoad in a method , Not call again such as [self viewDidLoad ];
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM