数组对象更新而不分配

Array Objects update without assign

我想将图像从 Gallery/Camera 添加到 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"]]];

我正在将 NSMutableDictionary 对象添加到 ViewController A 中的一个全局数组中并将其发送到 ViewController B,其中我 add/update 图片来自 gallery/camera.

但是当我按下后退按钮并返回 UIViewController A 然后再次转到视图控制器 B 时,那些图像仍在该数组中。

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

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

- (IBAction)goToBack:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

谁能告诉我哪里出错了?

创建另一个数组并将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];
 }

最后,通过复制 NSObject class 的对象并添加它的数组来解决。

[[controllerb setDocuments1]addObjectsFromArray:array];