开发者

Count of NSMutableArray is 0 after adding object

hi i have an object (objectA) with tow instance variables, NSString and NSDate. and i have two other objects one with tableView and add button (objectB) and one presented modally when you press the add button (objectC) and in this object view you can type name and date and when this object (objectC) dismissed i create new object (objectA) with name and date.

objectB have NSMutableArray and i want to add objectA to this array so it can be appear in tableView and i do it like this in objectC.m

- (IBAction)saveButtonPressed {
   objectA *a = [[objectA alloc] init开发者_开发知识库];
   [a setName:[myUITextField text]];
   [a setDate:[myDatePicker date]];

   objectB *b = [[objectB alloc] init];
   [[b myMutableArray] addObject:a]];
   [[b myMutableArray] count]; // count == 1 here but when i go back to objectB implementation it will be 0

}

and the app crash here

any ideas ?

thanks,

edit: crash is gone i just edit the init method of objectA but myMutableArray still 0

in objectB.m

- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

        UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self 
                                                                             action:@selector(addBarButton:)];
        [navItem setRightBarButtonItem:bbi];
        [bbi release];

        myMutableArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addBarButton:(id)sender {
    myObjectC = [[objectC alloc] init];
    [self presentModalViewController:myObjectC animated:YES];

}

i also have in objectB.h

@property (nonatomic, retain) NSMutableArray *myMutableArray;

the count of myMutableArray in objectC in saveButtonPressed method is now 1 but when i back to objectB tableView where i want to display the myMutableArray is still 0

Edit2 : after i put a lot of NSLog i found that when i make new objectB in saveButtonPressed: method the count of myMutableArray will be 1 but when i go back to my tableView (objectB) myMutableArray is back to 0 maybe because i create new and separate object of objectB in objectC (saveButtonPressed:) also if i don't alloc and init the objectB in saveButtonPressed: method objectB will be nil and i cant put objectA in myMutableArray

so i guess i have to get pointer to original objectB but how?


Since count == 0 after you added an object, I'm guessing it's one of two things:

  1. [b myMutableArray] is returning nil because you forgot to allocate myMutableArray in the init function for objectB (e.g. myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];)

  2. [b myMutableArray] is returning a new mutable array each time it's called. Perhaps you are doing something like:

- (NSMutableArray *)myMutableArray {
  return [NSMutableArray arrayWithCapacity:10];
}

Sounds like (1) is probably the likely cause but it still doesn't explain the crashing. What error messages do you see in the debug console when the crash happens?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜