0
您好在iPhone应用程序中的单身人士类有问题。 我为NSString值的可视化创建了一个简单的类。 我的问题是当我尝试在textVIew中标记NSString时生成的问题。 我打电话给我的方法和Singleton类中的字符串的值是(无效的)(我测试过它与调试)。 你能帮我解决代码吗?SIngleton在iPhone应用程序中的价值问题
我的代码:
#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