数组对象更新而不指定

时间:2016-07-06 18:46:33

标签: ios uitableview uiimagepickercontroller nsmutabledictionary nsobject

我想将图库/相机中的图像添加到NSMutableDictionary&我正在使用以下方法找到Here

[dict setObject:UIImageJPEGRepresentation(chosenImage,0.5) forKey:@"image_four"];

[documents replaceObjectAtIndex:0 withObject:dict];

[tableOne reloadData];

在索引路径的行中的单元格

[cell.oneImgView setImage:[[UIImage alloc]initWithData:[dict objectForKey:@"image_four"]]];

我在ViewController A& A中添加了NSMutableDictionary对象到一个全局数组中。将它发送到ViewController B,我在其中添加/更新来自图库/相机的图像。

但是当我按下后退按钮并返回UIViewController A然后再次查看控制器B时,那些图像仍在该阵列中。

简而言之,每当我将图像添加到UIViewController B的数组时,它会对UIViewController A的数组产生一些影响。

我希望在完成按钮单击时完成此操作,但如果我单击导航栏的后退按钮,它将与完成按钮相同。

- (IBAction)goToBack:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

有人能告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

创建另一个数组并将ViewController数组分配给此数组

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ 
   ViewControllerB *controllerb = [segue destinationViewController];
   [controllerb setDocuments1:sender];
}

ViewControllerB.h

@property (nonatomic, strong) NSMutableArray *documents1;

ViewController.m

#import "ViewControllerB.h"
#import "UploadCell.h"

@interface ViewControllerB ()
{
   NSArray *arrSavedObjects;
}

@end

@implementation ViewControllerB
@synthesize documents,documents1;


-(void)viewWillAppear:(BOOL)animated
{
   arrSavedObjects = [[NSUserDefaults standardUserDefaults] objectForKey:@"arrayObjects"];
   documents = [NSMutableArray arrayWithArray:arrSavedObjects];
   if(documents.count==0)
   {
      documents = documents1;
   }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
   UIImage  *chosenImage = info[UIImagePickerControllerOriginalImage];
  //NSMutableDictionary *dict = [documents objectAtIndex:selected];
  NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[documents objectAtIndex:selected]];
  if (![dict objectForKey:@"image1"]) {
    [dict setObject:UIImageJPEGRepresentation(chosenImage, 0.5) forKey:@"image1"];
  }
  else{
    [dict setObject:UIImageJPEGRepresentation(chosenImage, 0.5) forKey:@"image2"];
  }
  [documents removeAllObjects];
  [documents addObject:dict];

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [defaults setObject:documents forKey:@"arrayObjects"];
   [defaults synchronize];

   [tableDocuments reloadData];
   [controller dismissViewControllerAnimated:YES completion:nil];
 }

答案 1 :(得分:0)

最后,通过复制NSObject类和&amp;的对象来解决。添加数组。

[[controllerb setDocuments1] addObjectsFromArray:array];