views:

190

answers:

2

Hi,

I have a root view controller that I'm adding a subviewcontroller to.

Rootviewcontroller: I'm trying to add a value from a dictionary as the sole member of the moviewController.list NSMutableArray. However its not getting added and the NSlog is telling me that the "list: (null)"

for (id key in dictionary) {

    MovieController *movieController = [[MovieController alloc] initWithStyle:UITableViewStylePlain];
    movieController.title = key;


    [movieController.list addObject:[dictionary objectForKey:key]];
    NSLog(@"list: %@",[movieController.list count]); //<<<<


    [array addObject:movieController];
    [movieController release];
}

in the subviewcontroller (MovieController) I create the list thats used to display the items in the table as:

- (void)viewDidLoad {


    NSMutableArray *array = [[NSMutableArray alloc] init];
    self.list = array;

    [array release];
    [super viewDidLoad];
}

Why is it that I can't addobject in the rootviewcontroller? The only way I can get the list to display in the MovieController is to initWithObjects at the time of instantiation.

Regards

Sapatos

A: 

The -count returns an integer, not an ObjC object, so you should never format it with %@. Use %d instead:

NSLog(@"list: %d",[movieController.list count]); //<<<<
KennyTM
A: 

You are initializing the list property in the -viewDidLoad method but this method is not called until the view is loaded. Since the -viewDidLoad method is not called, list remains uninitialized at the time you add the object as follows,

[movieController.list addObject:[dictionary objectForKey:key]];

I would suggest you initialize the list property in the initializer rather so that the list is initialized by the time you add the object.

Deepak