0

I have a dictionary with three objects, and I want to add this dictionary to an NSMutableArray in a previous view of the stack. This array is correctly synthesized in the mainviewcontroller. However when I try to

[mainVC.fotoObjectArray addObject:[NSMutableDictionary dictionaryWithDictionary:fotoObject]]; Nothing gets added to the array.

It seems like I didnt alloc/init it correctly, but it's retained in a property in the previous view controller.

A user takes a picture, or selects from the album with a button. When he returns:

PhotoViewController.m

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

NSArray *viewControllers = [self.navigationController viewControllers];
MainViewController *mainVC = (MainViewController *)[viewControllers objectAtIndex:viewControllers.count - 2];
[mainVC setFotoGenomen:@"Ja"];

i = @"foto";
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[fotoObject setObject:[NSData dataWithData:imageData] forKey:@"image"];

[mainVC.fotoObjectArray addObject:[NSMutableDictionary dictionaryWithDictionary:fotoObject]];

//display image
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setFrame:CGRectMake(10.0f, 120.0f, 300.0f, 300.0f)];
imageView.backgroundColor = [UIColor whiteColor];
[[self view] addSubview:imageView];
[imageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];
[imageView release];

}

At the same time the GPS gets logged at PhotoViewController.m when the view gets loaded

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation 
{ 
CLLocationCoordinate2D location = newLocation.coordinate; 

fotoObject = [[NSMutableDictionary alloc] init];
[fotoObject setObject:[NSNumber numberWithDouble:location.latitude] forKey:@"latitude"];
[fotoObject setObject:[NSNumber numberWithDouble:location.longitude] forKey:@"longitude"];

} 

The 'fotoObject' dict contains the right keys and values. Now it just needs to put that dictionary in the NSMutableArray of MainViewController.m.

4
  • Check in the debugger that mainVC is actually the object you think it is - if you log mainVC.fotoObjectArray do you see anything? Are there any other objects in there? Commented Oct 12, 2011 at 7:19
  • Nope, when I log that, it returns null Commented Oct 12, 2011 at 7:22
  • mainVC, or mainVC.fotoObjectArray? Commented Oct 12, 2011 at 7:23
  • mainVC.fotoObjectArray returns null right after inserting the dictionary, and also returns null back in the mainviewcontroller. Commented Oct 12, 2011 at 7:33

1 Answer 1

1

It sounds like you never initialise the array in mainVC. If you've just got synthesised accessors then they will return nil unless you have explicitly set up the array.

In mainVC's viewDidLoad method you could have something like:

if (!self.fotoObjectArray)
    self.fotoObjectArray = [NSMutableArray array];

This will ensure that you actually have an array to add things to.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That was it! I knew it was something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.