2011-10-12 37 views
0

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

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

} 

在当视图被加载

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

} 

的GPS得到的PhotoViewController.m登录同一时间 'fotoObject' 字典包含正确键和值。现在只需要将该字典放在MainViewController.m的NSMutableArray中。

+0

检查调试器mainVC实际上是你认为它的对象 - 如果你记录mainVC.fotoObjectArray你会看到什么东西吗?那里有其他物体吗? – jrturton

+0

不,当我登录时,它返回null – MaikelS

+0

mainVC或mainVC.fotoObjectArray? – jrturton

回答

1

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

在mainVC的viewDidLoad方法,你可以有这样的:

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

这将确保你确实有一个数组的东西添加到。

+0

谢谢!就是这样!我知道这是这样的。 – MaikelS

相关问题