Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-14-2011, 09:03 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default Problem with adding data to a singleton mutablearray

Hi, i have a problem that i can't figure out. With some help from some nice guys in here i figured out how to use a singleton class. In the singleton class i have a mutablearray. The arra is shared to a class which have a tableview. There is a barbuttonitem which push you to a modal controller and when you hit save in that view a new object should be added to the array and tableview.

My class with the tableview looks like this:
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [DataContainerSingleto sharedDataContainerSingleto].arrayForSaved = [[NSMutableArray alloc] init];

}

- (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 = [[DataContainerSingleto sharedDataContainerSingleto].arrayForSaved objectAtIndex:indexPath.row];
	
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[DataContainerSingleto sharedDataContainerSingleto].arrayForSaved count];
}
i set my singleton array as the text in cellForRowAtIndex and make it count in numberOfRowsInSection. In the view did load method i alloc it.

In my modal controller the code looks like this when i hit the save button.

Code:
-(IBAction)save {
    
    [DataContainerSingleto sharedDataContainerSingleto].arrayForSaved = [[NSMutableArray alloc] init];
    
    [[DataContainerSingleto sharedDataContainerSingleto].arrayForSaved addObject:@"Test"];
    
    [self dismissModalViewControllerAnimated:YES];
}
Can someone help we figure out what is wrong please

- Jonas
DrJones is offline   Reply With Quote
Old 07-14-2011, 09:14 AM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

If you did that, you'd be overriding the array every single time you saved. Alloc the array in your singleton init method. Then just call addObject.
Domele is offline   Reply With Quote
Old 07-14-2011, 11:06 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
If you did that, you'd be overriding the array every single time you saved. Alloc the array in your singleton init method. Then just call addObject.
Okay, should i just call: -(id) init {

arrayForSaved = [[NSMutableArray alloc] init];
}

but then it says that its a non-void function. What should i do? i tried to return 0 but then it crashes when i run the save function
DrJones is offline   Reply With Quote
Old 07-14-2011, 03:13 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Your method is missing the return value. Read up on initializing objects: Loading…
baja_yu is offline   Reply With Quote
Old 07-14-2011, 04:12 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Your method is missing the return value. Read up on initializing objects: Loading…
Ahh, now my init method looks like this and when i push save in the modal controller it will just dismiss the modal view and show a empty tableview

-(id)init {

self =[super init];
If (self = super init) {
arraysForSaved = [[NSMutableArray alloc]init];
}
return self;
}

In my save function i do:

[[DataHandlerSingleto sharedDataHandlerSingleton].arrayForSaved addObject:@"Test"];

And then i dismiss the view. In my tableview class i make the singleton array count and cellForRow i use this to set the different rows:

cell.textLabel.text = [[DataHandlerSingleto sharedDataHandlerSingleton].arrayForSaved objectAtIndex:indexPath.row];

I can't figure out what im doing wrong
DrJones is offline   Reply With Quote
Old 07-14-2011, 04:28 PM   #6 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

What does the method sharedDataHandlerSingleton look like? Post the code.
Domele is offline   Reply With Quote
Old 07-14-2011, 04:50 PM   #7 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
What does the method sharedDataHandlerSingleton look like? Post the code.
In DataHandlerSingleto.h:

+(DataHandlerSingleto *)sharedDataHandlerSingleton;
DrJones is offline   Reply With Quote
Old 07-14-2011, 04:51 PM   #8 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Post the code inside the method.
Domele is offline   Reply With Quote
Old 07-14-2011, 04:57 PM   #9 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

It doesnt have anything inside, maybe thats the problem. The reference i looked at just says that i need to have a global function.
DrJones is offline   Reply With Quote
Old 07-14-2011, 05:15 PM   #10 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

If you thought that calling an empty method would accomplish anything, you need to learn more about Objective C or programming in general.

Here is a tutorial on creating a Singleton: The Objective-C Singleton
Domele is offline   Reply With Quote
Old 07-15-2011, 12:07 PM   #11 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

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?
DrJones is offline   Reply With Quote
Old 07-15-2011, 04:31 PM   #12 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

First you are going to leak your property. It won't matter because it's a singleton but still. First, if you are going to alloc a property do it with this 3 step pattern:
Code:
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
self.arrayForSavedData = mutableArray;
[mutableArray release];
And then you have to release it in your dealloc method:
Code:
[arrayForSavedData]; //one way to do it
self.arrayForSavedData = nil //another way to do it
Try calling reloadData on your table view.
Domele is offline   Reply With Quote
Reply

Bookmarks

Tags
add objects, array, singleton, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 368
9 members and 359 guests
apatsufas, chemistry, Kirkout, leostc, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0