BOOL重置在init方法来无 [英] BOOL being reset to NO in init method

查看:265
本文介绍了BOOL重置在init方法来无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在一个UINavigationController的酒吧正在播放按钮。

I want to show a Now Playing button in a UINavigationController's bar.

我有我使用的跟踪是否音频文件正在播放一个类(NowPlayingManager)。我用张贴在另一个类(AudioViewController)的通知,指示播放状态。 AudioViewController创建具有分配/ init和释放它的NowPlayingManager的一个实例。在接到通知之NowPlayingManager的目标我设置NowPlayingManager的isNowPlaying布尔值YES。

I have a class (NowPlayingManager) that I am using to keep track of whether an audio file is currently being played. I use a notification posted in another class (AudioViewController) to indicate playing status. AudioViewController creates an instance of the NowPlayingManager with alloc/init and releases it. In NowPlayingManager's target of the received notification I set NowPlayingManager's isNowPlaying Boolean to YES.

在音频停止播放我发一个通知,它设置isNowPlaying布尔为NO。

When the audio stops playing I send another notification that sets the isNowPlaying bool to NO.

然而,每个类初始化的布尔时间设置为NO,这是有意义的,因为它是NowPlayingManager的一个新实例,从不显示现在播放按钮。

However, each time the class is initialized the bool is set to NO, which makes sense because it is a new instance of NowPlayingManager and the Now Playing button is never displayed.

我怎样才能获得isNowPlaying布尔通过我NowPlayingManager的所有实例坚持?或者说,如果我有应用程序代理初始化的NowPlayingManager而不是AudioViewController,以便只有一个实例被创建?

How can I get the isNowPlaying bool to persist through all instances of my NowPlayingManager? Or, rather, Should I have the app delegate init the NowPlayingManager rather than AudioViewController so that only one instance is created?

推荐答案

当然,你可以定义为isNowPlaying类成员 +(BOOL)isNowPlaying 。看到
<一href=\"http://stackoverflow.com/questions/844958/objective-c-how-to-delcare-a-static-member-that-is-visible-to-subclasses\">Objective-C:如何delcare一个静态成员是可见的子类?,获取更多相关信息。

You can of course define the isNowPlaying as class member + (BOOL) isNowPlaying. See Objective-C: how to delcare a static member that is visible to subclasses? for more information on this.

但是,正如你已经说好像是只创建一个实例,即Singleton模式更有用。我建议对单身人士和他的下载SynthesizeSingleton.h马特Galaghers发帖:

But as you said already it seems to be more useful to create just one instance i.e. the singleton pattern. I suggest Matt Galaghers posting about singletons and his downloadable SynthesizeSingleton.h:

http://cocoawithlove.com/2008/11/单身-appdelegates和机顶level.html

这篇关于BOOL重置在init方法来无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆