1

I have two view controllers. First one is Custom view controller that loads the images from asset library. Second view controller shows the full size of selected image with cancel & Delete button

I have used the below code for delete the selected image from custom view controller. customviewcontroller.m

-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths
{
//here i want to control the delete option when cancel pressed
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *itemPath in itemPaths) {
[indexSet addIndex:itemPath.row];
}
[self.selectedAssets removeObjectsAtIndexes:indexSet];
}
/* call the delete function*/
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
    NSArray *selectedItemsIndexPaths = [self.collectionview1 indexPathsForSelectedItems];
    [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];
    [self.collectionview1 deleteItemsAtIndexPaths:selectedItemsIndexPaths];
    }

This working fine for delete the selected image from custom view controller. but It works also cancel. Now, I want to control deletion on cancel. kindly help me to solve this.

I have already tried using button tag for identify which button was pressed. but can't control in custom vc secondview.m

- (IBAction)CancelPhoto:(id)sender{
 [Cancel setTag:1]; //set tag value at cancel
}
Ashwin C
  • 11
  • 2

2 Answers2

1

There are several ways to pass date between view controllers. I'm giving you a very simple solution as per your requirements:

Assuming you have all your images in a mutable array in first view controller.

NSMutableAray *imagesArray;

Make a property images array of NSMutableArray type in second view controller.

@property(nonatomic, strong) NSMutableAray *imagesArray;

assign your array to property while pushing/presenting view

secondController.imagesArray = imagesArray;

On delete event remove that image from array.

[self.imagesArray removeObjectAtIndex:selectedIndex];

dismiss/pop your second view where ever you want and refresh your first view controller in viewWillAppear of viewDidAppear method.

As you have passed reference of your main images array, both classes (view controllers) share same array through pointer and a change in array from either side will be reflected in both screens

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
0

There are 3 ways (in my opinion):

  1. Delegate
  2. Notification
  3. In case: you are using database, just remove image url and reload data
HoanNguyen
  • 405
  • 2
  • 12