0

I'm having trouble trying to delete multiple objects selected in an UICollectionView from my own UIActivity.

FilesCollectionController.h

@property(nonatomic,weak) IBOutlet UICollectionView *collectionView;

This collection view uses file paths to show images.

FilesCollectionController.m

...  
ActivityTrash *trash = [[ActivityTrash alloc] init];
NSMutableArray *customActivities = [[NSMutableArray alloc] initWithObjects:trash, nil];

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:objectsSelected applicationActivities:customActivities];
UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:controller];
...
// Present the Navigation controller
[self presentViewController:destNav animated:YES completion:nil];

ActivityTrash.h

@interface ActivityTrash : UIActivity 

@property(nonatomic,retain) NSMutableArray *objectsToDelete;

@end

ActivityTrash.m

@implementation ActivityTrash

- (NSString *) activityTitle {
    return @"Delete";
}

- (UIImage *) activityImage {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return [UIImage imageNamed:@"ipadTrash"];
    }
    else
    {
        return [UIImage imageNamed:@"iphoneTrash"];
    }
}

- (BOOL) canPerformWithActivityItems:(NSArray *)activityItems
{
    return ([activityItems count] != 0);
}

- (void) prepareWithActivityItems:(NSArray *)activityItems
{
    self.objectsToDelete = [[NSMutableArray alloc] init];
    for (NSURL *url in activityItems) {
        [self.objectsToDelete addObject:[url path]];
    }
}

- (UIViewController *) activityViewController
{
    return nil;
}


- (void)performActivity {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete file(s)"
                                                    message:@"Are you sure ?"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // If the user pressed ok, delete each image which path is in objectsToDelete
    if (buttonIndex == 1) {
        for (NSString *pathImage in self.objectsToDelete) {
            [DocumentManager deleteImageWithPath:pathImage];
        }
    }
    [self activityDidFinish:YES];
}

Files are actually deleted. But the problem is that I would like to call [self.collectionView reloadData] in my FilesCollectionController right after UIActivity did finish, and so my collection view would be updated immediately...

1 Answers1

0

Well, I found a way to achieve it :

ActivityTrash.h

@class FilesCollectionController;
@interface ActivityTrash : UIActivity {
    FilesCollectionController *collectionVC;
}

@property(nonatomic,retain) NSMutableArray *objectsToDelete;

- (id)initFromViewController:(FilesCollectionController *)vc;

@end

ActivityTrash.m

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // If the user pressed ok
    if (buttonIndex == 1) {
        for (NSString *pathImage in self.objectsToDelete) {
            [DocumentManager deleteImageWithPath:pathImage];
        }
    }

    [collectionVC viewWillAppear:YES];
    [self activityDidFinish:YES];
}

FilesCollectionController.m

ActivityTrash *trash = [[ActivityTrash alloc] initFromViewController:self];

May not be the best solution but it works.