从另一个视图将对象添加到NSMutableArray

时间:2011-10-12 07:07:38

标签: iphone nsmutablearray

我有一个包含三个对象的字典,我想将这个字典添加到堆栈的上一个视图中的NSMutableArray。该数组在mainviewcontroller中正确合成。但是,当我尝试

[mainVC.fotoObjectArray addObject:[NSMutableDictionary dictionaryWithDictionary:fotoObject]];没有任何内容添加到数组中。

好像我没有正确地分配/初始化它,但它保留在前一个视图控制器的属性中。

用户拍摄照片,或使用按钮从相册中选择。当他回来时:

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];

}

同时,当视图加载时,GPS会在PhotoViewController.m中记录

-(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"];

} 

'fotoObject'字典包含正确的键和值。现在它只需要将该字典放在MainViewController.m的NSMutableArray中。

1 个答案:

答案 0 :(得分:1)

听起来你永远不会在mainVC中初始化数组。如果您刚刚获得了合成访问器,那么除非您明确设置了数组,否则它们将返回nil

在mainVC的viewDidLoad方法中,您可以使用以下内容:

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

这将确保您实际上有一个数组来添加内容。