In your Singleton class, you may have the method like + (MySingleton *) instance
So use [[Singleton instance] audioPlayer stop];
or
Singleton *singleton = [Singleton instance];
[singleton.audioPlayer stop];
Example MySingleton with NSString
MySingleton *mySingleton = [MySingleton instance];
mySingleton.myString = @"hi sigletone";
NSLog(@"%@",mySingleton.myString);
Example MySingleton.h
@interface MySingleton : NSObject
@property (nonatomic,retain) NSString *myString;
+ (MySingleton *) instance;
@end
Example MySingleton.m
#import "MySingleton.h"
@implementation MySingleton
- (id) initSingleton
{
if ((self = [super init]))
{
// Initialization code here.
}
return self;
}
+ (MySingleton *) instance
{
// Persistent instance.
static MySingleton *_default = nil;
// Small optimization to avoid wasting time after the
// singleton being initialized.
if (_default != nil)
{
return _default;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
// Allocates once with Grand Central Dispatch (GCD) routine.
// It's thread safe.
static dispatch_once_t safer;
dispatch_once(&safer, ^(void)
{
_default = [[MySingleton alloc] initSingleton];
});
#else
// Allocates once using the old approach, it's slower.
// It's thread safe.
@synchronized([MySingleton class])
{
// The synchronized instruction will make sure,
// that only one thread will access this point at a time.
if (_default == nil)
{
_default = [[MySingleton alloc] initSingleton];
}
}
#endif
return _default;
}
@end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…