我一直在寻找 tableviews 和单例类,但找不到任何解决方案,所以我在这里。
当用户选择一行时,我在视图控制器中有一个表格视图,我想将所选数据发送到单例类中的数组并将其打印到另一个视图控制器的屏幕上。
这是我的单例类代码:
#import <Foundation/Foundation.h>
@interface DataController : NSObject {
NSArray* standLoc;
}
@property (readonly)NSArray* standLoc; // stand location
+(DataController*)sharedInstance;
@end
#import "DataController.h"
@implementation DataController
@synthesize standLoc;
+(DataController*)sharedInstance
{
static DataController* sharedInstance = nil;
if (!sharedInstance)
{
sharedInstance = [[DataController alloc]init];
}
return sharedInstance;
}
@end
那么我应该如何将数据传递给我尝试遵循的单例类中的数组,
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
StartHuntViewController *startHuntController = [[StartHuntViewController alloc] initWithNibName:@"StartHuntView" bundle:nil];
DataController* sharedSingleton = [DataController sharedInstance];
sharedSingleton = [stands objectAtIndex:indexPath.row];
startHuntController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:startHuntController animated:YES];;
[startHuntController release];
startHuntController =nil;
}
在调试中,我可以看到所选项目在 sharedSingleton 中,但我怎样才能将它传递给 NSArray* standLoc?
编辑
所以我编辑了我的代码,它现在可以正常工作多个控制器视图
我的单身 .m 和 .h :
#import <Foundation/Foundation.h>
@interface DataController : NSObject {
NSString* standLoc;
}
@property (nonatomic,retain)NSString* standLoc; // stand location
+(DataController*)sharedInstance;
-(void) setData: (NSString *) data;
@end
#import "DataController.h"
@implementation DataController
static DataController* sharedInstance = nil;
@synthesize standLoc;
+(DataController*)sharedInstance
{
@synchronized (self) { //this ensure this methods will not be called at the same time..
if(sharedInstance == nil){
[[self alloc] init];
}
}
return sharedInstance;
}
+(id) allocWithZone:(NSZone *)zone{
@synchronized (self){
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
-(id)copyWithZone:(NSZone *)zone{ // incase if we want to copy our singleton instance
return self;
}
//to protect singleton from deallocation, we need to override some functions of memory allocation
-(id) retain {
return self;
}
-(id) autorelease{
return self;
}
-(NSUInteger ) retainCount{
return NSUIntegerMax;
}
-(id) init{ // lets set the default data in it
@synchronized (self){
[super init];
standLoc = [[NSString alloc] initWithString:@"Stand Loc"];//for performance, as we expect 5 digits from server, it's size was set to another 5 digits..
return self;
}
}
-(void) setData: (NSString *) data{ // this is the function to set static data which is the member of the class, reaching data will be allowed with this method
@synchronized (self){
if (standLoc != data) {
[standLoc release];
standLoc = [data retain];
}
}
}
-(NSString *) standLoc{
@synchronized(self){
return standLoc;
}
}
将数据传递给单例:
DataController* sharedSingleton = [DataController sharedInstance];
NSString* transfer = [stands objectAtIndex:indexPath.row];
[sharedSingleton setData:transfer];