ios iPhone:viewDidDisappear 的正确用法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5204489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: What is the correct usage of viewDidDisappear?
提问by seeafish
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.
我对 Objective C 还是很陌生,我想知道关于 viewDidDisappear 的一些事情。我有一个播放声音的应用程序(使用 AVAudioPlayer),我想在切换视图时停止声音。
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!
它工作正常。但是我脑子里的小程序员说我没有正确使用它。我很确定你应该用一个布尔参数调用 viewDidDisappear,而不是仅仅指定 (BOOL)animated; 此外,在我的视图切换中有一些动画会很好......再说一次,这可能是一个完全不同的讨论!
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.
那么,我做错了什么,我将如何正确使用它?我是否必须链接呼叫按钮操作?实际声明函数本身的正确玩法在哪里?谢谢。
回答by petershine
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:
我实现了viewDidDisappear:(BOOL)animated
EXTENSIVELY,以及viewWillAppear
,viewWillDisappear
和viewWillDisappear
实现这个方法的主要原因是让你的视图控制器在事件中做一些事情,比如viewDidDisappear
你不调用这个方法,但你的应用程序会调用你的视图控制器来做已实现的事情那里。既然是继承的方法,只要保证从超类继承的所有实现都可以完成,实现就好了viewDidDisappear
。所以,我建议你把你的代码改成这样:
- (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];
}
回答by Anomie
- (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).
- (void)viewDidDisappear:(BOOL)animated
是一个方法声明,而不是任何类型的调用。UIKit 在操作视图控制器时调用该方法本身;您不需要自己调用它,除非您正在编写自己的代码,通过直接操纵它们控制的视图来使视图控制器出现和消失(例如,如果您出于某种原因重写 UINavigationController)。
You are doing something wrong, though: you must call [super viewDidDisappear:animated]
somewhere in your implementation, or things may break.
但是,您做错了:您必须[super viewDidDisappear:animated]
在实现中的某个地方调用,否则事情可能会中断。
回答by hotpaw2
The "small programmer" voice in your mind is probably more used to procedural coding, where you call the OS and tell it what to do. Cocoa Touch instead uses an event driven paradigm, where your program has routines (methods) that the OS(framework)calls when it is good and ready. viewDidDisappear is one of those routines. Just sit tight, and wait for the OS to call it (assuming you've set everything up properly.)
您脑海中的“小程序员”声音可能更习惯于程序编码,您可以在其中调用操作系统并告诉它该做什么。Cocoa Touch 改为使用事件驱动的范例,其中您的程序具有操作系统(框架)在运行良好并准备就绪时调用的例程(方法)。viewDidDisappear 就是这些例程之一。坐好,等待操作系统调用它(假设您已经正确设置了所有内容。)
回答by FreeAsInBeer
viewDidDisappear:
is an optional method that your view can utilize to execute custom code when the view does indeed disappear. You aren't required to have this in your view, and your code should (almost?) never need to call it.
viewDidDisappear:
是一种可选方法,当视图确实消失时,您的视图可以利用它来执行自定义代码。您不需要在您的视图中拥有它,并且您的代码应该(几乎?)永远不需要调用它。