2011-10-12 69 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

相關問題