Don't worry i know that compiling a empty method won't do anything, its because i downloaded a file, that did it for me so i should just call sharedMyClassName and then the file does it for me. But now i created my own singleton class, and it still won't work
SingletonData.h
Code:
#import <Foundation/Foundation.h>
@interface SingletonData : NSObject {
NSMutableArray *arrayForSavedData;
}
@property (nonatomic, retain) NSMutableArray *arrayForSavedData;
+(SingletonData *)sharedDataInstance;
@end
SingletonData.m
Code:
#import "SingletonData.h"
@implementation SingletonData
@synthesize arrayForSavedData;
+(SingletonData *)sharedDataInstance {
static SingletonData *sharedDataInstance;
@synchronized(self) {
if (!sharedDataInstance) {
sharedDataInstance = [[SingletonData alloc] init];
}
}
return sharedDataInstance;
}
-(id)init {
self = [super init];
if (!self) return nil;
self.arrayForSavedData = [[NSMutableArray alloc] init];
return self;
}
-(void)dealloc {
[super dealloc];
}
@end
Then i create a private in the delegate:
@private SingletonData *sharedData;
Launching with options i call:
sharedData = [SingletonData sharedDataInstance];
That should work fine. The annoying part comes when i create a object and put it into my array.
SavedCalculationsTableView.m
Code:
-(void)addSavedcalculation {
AddInterface *add = [[AddInterface alloc] initWithNibName:@"AddInterface" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// Set up the cell
cell.textLabel.text = [sharedData.arrayForSavedData objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [sharedData.arrayForSavedData count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
then it pushes the modal controller AddInterface.m
Code:
-(IBAction)save {
[sharedData.arrayForSavedData addObject:@"Test"];
[self dismissModalViewControllerAnimated:YES];
}
when i click save nothing happens its just a plain tableview, its like the cellForRow doesnt work together with a singleton array because if i create a normal array everything works fine. What should i do?