从另一个视图将对象添加到 NSMutableArray
我有一个包含三个对象的字典,我想将该字典添加到堆栈的上一个视图中的 NSMutableArray 中。该数组在主视图控制器中正确合成。但是,当我尝试
[mainVC.fotoObjectArray addObject:[NSMutableDictionarydictionaryWithDictionary: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 中即可。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你从来没有在 mainVC 中初始化数组。如果您刚刚获得了合成访问器,那么除非您显式设置了数组,否则它们将返回 nil。
在 mainVC 的 viewDidLoad 方法中,您可以有类似的内容:
这将确保您实际上有一个可以添加内容的数组。
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:This will ensure that you actually have an array to add things to.