English 中文(简体)
BOOL正在采用惯用法改用NO。
原标题:BOOL being reset to NO in init method

我想在UINavigationController的酒吧展示一个现在的游乐纽。

我有一个班子(NowPlayingManager),我正在利用这个班子来跟踪一个声音档案是否正在发挥作用。 我使用另一班(AudioViewController)上张贴的通知来表明竞争状况。 录音记录员对现在PlayingManager的节目和节目进行播放。 在目前PlayingManager, 收到通知的目标,我向YES投了通知。

当音频站起作用时,我又向NowPlaying bool发送了通知。

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.

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?

最佳回答

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

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/singletons-appdelegates-and-top-level.html

问题回答

If I understand correctly, you should use NSUserDefaults. This remembers it even if you change class, close the app, etc.

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

之后,将 b子检查成:

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

{

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

}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签