2012-08-02 61 views
0

我有一個多選擇器,我把它存儲在一個數組中。它保留在應用程序使用中。但是,當應用程序退出時,它將刪除選擇視圖。我想要的是將它保存到一個數組後,我將它保存在NSUserDefaults,所以當我打開應用程序的選擇仍然會在那裏。我如何使用NSUserDefaults作爲整體存儲索引,然後再次爲拾取器加載索引。保存整數/索引到NSUserDefaults

這裏是我的imagepicker視圖部分的負載:

- (void)loadAssets 
{ 
    count = 0; 
    [self.assets removeAllObjects]; 

    AGIPCAssetsController *blockSelf = self; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 

     selectedPhotos = [[NSMutableArray alloc] initWithArray:blockSelf.imagePickerController.selection]; 

     @autoreleasepool { 
      [blockSelf.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 
       //ALAssetRepresentation* representation = [result defaultRepresentation]; 
       // NSUInteger assetindex= [blockSelf.assetsGroup numberOfAssets]; 

       if (result == nil) 
       { 
        return; 
       } 



       AGIPCGridItem *gridItem = [[AGIPCGridItem alloc] initWithAsset:result andDelegate:blockSelf]; 
       if(count < blockSelf.imagePickerController.selection.count) 
       { 
        if (blockSelf.imagePickerController.selection != nil && 
         [result isEqual:[selectedPhotos objectAtIndex:count]]) 
        { 

         gridItem.selected = YES; 
         count++; 

        } 
       } 
       [blockSelf.assets addObject:gridItem]; 

      }]; 
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      [blockSelf reloadData]; 

     }); 

    }); 

回答

0

保存陣列

[[NSUserDefaults standardUserDefaults] setObject:yourArray forKey:@"KeyName"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

然後加載陣列

yourArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"KeyName"] mutableCopy]; 

確保關鍵名稱是相同的