0

I have a multiple picker, which I store it's selection in an array. It is retained within the app usage. But when the app is exited, it removes the selection view. What i wanted is after saving it to an array, Im going to save it in NSUserDefaults so when I open the app the selection will still be there. How can I store the index as a whole using NSUserDefaults then load it again for the picker.

Here is my loading of the imagepicker view part:

- (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];

        });

    }); 
4

1 回答 1

0

保存数组

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

然后加载数组

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

确保键名相同

于 2012-08-02T07:59:28.507 回答