开发者

Add object to an NSMutableArray from another view

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.navigationContro开发者_Go百科ller 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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜