I have been searching for tableviews and singleton classes but could not find any solution so here I am.
I have a table view in a viewcontroller when user selects a row I want to send the selected data to an array in singleton class and print it to screen in another viewcontroller.
Here is my Singleton Class code:
#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
So how do Should I pass the data to the Array in the Singleton Class I tried following,
- (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;
}
In debug I can see selected item is in sharedSingleton but how can I pass it to NSArray* standLoc?
EDIT
SO i have edited my code and it works fine fow multiple contorller views right now
my Singleton .m and .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;
}
}
to pass data to singleton :
DataController* sharedSingleton = [DataController sharedInstance];
NSString* transfer = [stands objectAtIndex:indexPath.row];
[sharedSingleton setData:transfer];
The kind of syntax you want (after settling the above two points) is:
DataController* sharedSingleton = [DataController sharedInstance];
sharedSingleton.standLoc = // Whatever the data element is that should be in the singleton;