Question

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. please help me.

Thanks in advance!!

Was it helpful?

Solution 2

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:

[[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

- (void) appBecameActive
{
    call your method here
}

P.S. From your comments + is for class methods. Which with a view controller is probably not what you want. Read about class methods versus object methods to continue your iOS education.

OTHER TIPS

viewDidLoad is always called by the system, and you ideally shouldn't call it manually. That said, one way to call it is to access the .view property of a UIViewController. This does call viewDidLoad, if it hasn't already been called.

If there is some piece of code that should run everytime a view appears, you should write your code in viewWillAppear or viewDidAppear

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top