Brandon
Brandon

Reputation: 2171

Call self.viewDidAppear when App comes to foreground

I would like to have my app call viewDidAppear again when the user brings the app to the foreground.

- (void)appReturnsActive{

    //THIS IS THE BIT THAT DOESNT WORK, BUT [self.viewDidLoad] DOES WORK
    [self.viewDidAppear];
}

I am creating appReturnsActive in my viewDidAppear method like this. It works well:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification 
object:nil];

I can do self.viewDidLoad, but I can't choose viewDidAppear. Does anyone know how I might get this to work?

Thank you!!

Upvotes: 2

Views: 3302

Answers (4)

Robert
Robert

Reputation: 38213

When using the containment API, use – beginAppearanceTransition:animated: and – endAppearanceTransition:

If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.

Calling addSubView will automatically trigger viewWillAppear: and viewDidAppear: if the view's viewController is a child view controller, therefore calling viewWillAppear: directly will trigger view will appearance method twice. Using beginAppearanceTransition:animated:and– endAppearanceTransition` will suppress the automatic behaviour so you only get it called once.

Upvotes: 1

tkanzakic
tkanzakic

Reputation: 5499

The method viewDidAppear: will be called automatically every time the view become visible, there is no need to call it by your self. If you have some code on this method that you need to run from other places I recommend you to add a new method containing those instructions. viewDidAppear: will also perform view related operations that can penalize the performance. All the ways, if you need to call it, you are missing the BOOL parameter, see the documentation.

Upvotes: 0

Groot
Groot

Reputation: 14251

You are simply calling it wrong. It should be

[self viewDidAppear:YES];

However, one should never call this or any of the viewDidLoad, viewWillAppear and so on. Put the code you want to run in viewDidAppear:(BOOL) in a separate method and call that method.

hope it helps!

Upvotes: 6

melsam
melsam

Reputation: 4977

Have you tried [self viewDidAppear:YES] or [self viewDidAppear:NO] ?

Upvotes: 3

Related Questions