BOOL - reset - NO in init method

I want to show the Now Playing button in the UINavigationController panel.

I have a class (NowPlayingManager) that I use to track whether an audio file is currently playing. I am using a notification sent in another class (AudioViewController) to indicate playback status. AudioViewController creates an instance of NowPlayingManager with alloc / init and frees it. In the NowPlayingManager of the received notification, I set NowPlayingManager isNowPlaying Boolean to YES.

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

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

How can I get the isNowPlaying bool to persist through all instances of my NowPlayingManager? Or rather, Should I delegate the init application for NowPlayingManager, not the AudioViewController, to create only one instance?

0
source share
2 answers

Of course, you can define the isNowPlaying element as a class + (BOOL) isNowPlaying. See Objective-C: how to declare a static member that is visible to subclasses? for more information about this.

, , , singleton. , SynthesizeSingleton.h:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

0

, NSUserDefaults. , , ..

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KeyName"];

bool :

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"KeyName"] == YES]

{

    //Do whatever you would do when the bool is equal to YES

}

, !

0

All Articles