1

我想從畫廊/攝像頭添加圖片到NSMutableDictionary &我使用,我發現Here數組對象更新未指派

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

[documents replaceObjectAtIndex:0 withObject:dict]; 

[tableOne reloadData]; 

細胞用於排在Indexpath

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

我下面的方法m將NSMutableDictionary對象添加到ViewController中的一個全局數組中A &將它發送到ViewController B,我從圖庫/相機添加/更新圖像。

但是當我按回按鈕並返回UIViewController A,然後再次去查看控制器B這些圖像仍然在該數組中。

總之,每當我添加圖片到UIViewController B的陣列它的一些如何影響到UIViewController A.

我想這是上進行點擊按鈕完成,但數組,如果我點擊導航欄上的後退按鈕它與完成按鈕相同。

- (IBAction)goToBack:(id)sender { 

    [self.navigationController popViewControllerAnimated:YES]; 
} 

有人能告訴我我哪裏出錯了嗎?

+0

你現在想做什麼? – user3182143

+0

什麼是這條線「但是當我按回按鈕,並返回UIViewController A,然後再次去查看控制器B這些圖像仍然在該數組中」? – user3182143

+0

我的問題是,當我將圖像數據添加到字典並將其更新到視圖控制器b的數組,然後它在前面的視圖控制器A的主數組中改變,沒有理由也沒有代碼做這樣的事情。 –

回答

0

最後,通過複製NSObject的對象解決類&添加數組。

[[controllerb setDocuments1] addObjectsFromArray:array];

+0

這是我最終在討論部分給你的。我給你NSObject類。 – user3182143

+0

不,我們可以在答案中看到,您將它保存到nsuserdefaults中,這對記憶沒有好處,我的方法完全不同。 –

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]; 
} 
+0

它不能解決問題,存儲這許多NSData是不好的做法,它會相對放慢應用程序。 –

+0

您要求將對象保存到數組中,更改後我們返回到視圖控制器A.然後,當我們回到視圖控制器B時,它應顯示保存的數據對象。對於我簡單保存到NSUserDefault中的一組數據。根據你的問題我設定了解決方案。你必須清楚地問你的問題。 – user3182143

+0

保存只有當我點擊完成(完成)按鈕不popviewcontorller(取消) –