Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problem with adding data to a singleton mutablearray

DrJonesDrJones Posts: 43Registered Users
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:

- (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.


-(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
Post edited by DrJones on

Replies

  • DomeleDomele Posts: 2,948Registered Users
    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.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • DrJonesDrJones Posts: 43Registered Users
    Domele;354722 said:
    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 :(
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Your method is missing the return value. Read up on initializing objects: Loading…
  • DrJonesDrJones Posts: 43Registered Users
    baja_yu;354866 said:
    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 :(
  • DomeleDomele Posts: 2,948Registered Users
    What does the method sharedDataHandlerSingleton look like? Post the code.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • DrJonesDrJones Posts: 43Registered Users
    Domele;354908 said:
    What does the method sharedDataHandlerSingleton look like? Post the code.
    In DataHandlerSingleto.h:

    +(DataHandlerSingleto *)sharedDataHandlerSingleton;
  • DomeleDomele Posts: 2,948Registered Users
    Post the code inside the method.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • DrJonesDrJones Posts: 43Registered Users
    It doesnt have anything inside, maybe thats the problem. The reference i looked at just says that i need to have a global function.
  • DomeleDomele Posts: 2,948Registered Users
    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
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • DrJonesDrJones Posts: 43Registered Users
    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

    #import <Foundation/Foundation.h>


    @interface SingletonData : NSObject {

    NSMutableArray *arrayForSavedData;
    }

    @property (nonatomic, retain) NSMutableArray *arrayForSavedData;

    +(SingletonData *)sharedDataInstance;

    @end


    SingletonData.m

    #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

    -(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

    -(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? :)
  • DomeleDomele Posts: 2,948Registered Users
    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:

    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    self.arrayForSavedData = mutableArray;
    [mutableArray release];

    And then you have to release it in your dealloc method:

    [arrayForSavedData]; //one way to do it
    self.arrayForSavedData = nil //another way to do it


    Try calling reloadData on your table view.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
Sign In or Register to comment.