Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: What is the correct usage of viewDidDisappear?

I'm still very new to Objective C, and I was wondering something regarding viewDidDisappear. I have an app that plays a sound (using AVAudioPlayer), and I want to stop the sound when the view is switched.

If I do this in my view controller implementation:

- (void)viewDidDisappear:(BOOL)animated {
    [self.audioPlayer stop];
}

it works fine. But the small programmer in my brain is saying that I'm not using this correctly. I'm pretty sure you are supposed to CALL viewDidDisappear with a boolean argument, rather than just specify (BOOL)animated; besides, it would be nice to have some animation in my view switching... then again, that might be a whole different discussion!

So, what am I doing wrong, and how would I correctly use this? Do I have to link the call a button action? Where is the correct play to actually declare the function itself? Thanks.

like image 625
seeafish Avatar asked Mar 05 '11 14:03

seeafish


People also ask

What is Viewdiddisappear?

viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again).

Should I call super viewDidAppear?

Not calling [super viewDidAppear] is indeed a bad idea. The base class for UIViewController has code in viewDidAppear that needs to be called or things won't work correctly.

Is viewDidAppear called everytime?

viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded.

What is UIViewController life cycle?

The LifecycleThe view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.


2 Answers

- (void)viewDidDisappear:(BOOL)animated is a method declaration, not a call of any sort. The method itself is called by UIKit as view controllers are manipulated; you don't need to call it yourself unless you're writing your own code that makes view controllers appear and disappear by directly manipulating the views they control (e.g. if you were rewriting UINavigationController for some reason).

You are doing something wrong, though: you must call [super viewDidDisappear:animated] somewhere in your implementation, or things may break.

like image 127
Anomie Avatar answered Oct 27 '22 22:10

Anomie


I implement viewDidDisappear:(BOOL)animated EXTENSIVELY, along with viewWillAppear, viewWillDisappear and viewWillDisappear The main reason to implement this method is to make your view controller to do something at the event, such as viewDidDisappear You don't call this method, but your app will call your view controller to do what's implemented there. Since this is inherited method, as long as you make sure all the inherited implementation from the super class can be done, it's great to implement viewDidDisappear. So, I suggest you to change your code to be like this:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:(BOOL)animated];    // Call the super class implementation.
    // Usually calling super class implementation is done before self class implementation, but it's up to your application.

    [self.audioPlayer stop];
}
like image 32
petershine Avatar answered Oct 27 '22 22:10

petershine