Salut avoir un problème avec Singleton Class dans l'application iPhone. J'ai créé une classe simple pour la visualisation de la valeur NSString. Mon problème est généré lorsque j'essaie de marquer un NSString dans un textVIew. J'appelle mes méthodes et la valeur de la chaîne dans la classe Singleton est (invalide) (je l'ai testé avec le débogage). Pouvez-vous m'aider avec la solution de code.Problème avec la valeur de SIngleton dans l'application iPhone
mon code:
#import "UntitledViewController.h"
#import "SingletonController.h"
@implementation UntitledViewController
@synthesize resetTextEvent;
@synthesize buttonSavePosition;
-(IBAction)stamp{
textEvent.text = [sharedController name];
}
- (void)viewDidLoad {
[super viewDidLoad];
sharedController = [SingletonController sharedSingletonController];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
#import "SingletonController.h"
@implementation SingletonController
@synthesize name;
static SingletonController *sharedController = nil;
+(SingletonController*)sharedSingletonController{
if(sharedController == nil){
sharedController = [[super allocWithZone:NULL] init];
}
return sharedController;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedSingletonController] retain];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
-(id)init{
self = [super init];
if (self != nil) {
name = [NSString stringWithFormat:@"hello"];
}
return self;
}
-(void) dealloc {
[super dealloc];
}
@end