1

I want to show a UIAlertController from the Cell of a UICollectionView.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

}];
[alert addAction:deleteAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];

The problem is that the cell doesn't have the [self presentViewController:alert animated:YES completion:nil]; method.

Maybe someone can help me?

Lennart
  • 1,560
  • 5
  • 20
  • 38
ViceBrot
  • 13
  • 2

2 Answers2

1

You can create a method in your viewController that contains your collection view and call the method from your cell. Something like this:

- (void)presentAlert {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    [alert addAction:deleteAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

And then call the [self presentAlert] method from wherever you like i.e. your didSelectItemAtIndexPath

Prientus
  • 733
  • 6
  • 15
  • Definitely better to do this, than try to instantiate a new copy of the alert controller for each cell that triggers it. – brandonscript Feb 22 '17 at 16:19
0

You can use delegate

CollectionCell.h

#import <UIKit/UIKit.h>

@class CollectionCell;

@protocol CollectionCellDelegate

- (void)showDataFromCell:(CollectionCell *)cell;

@end


@interface CollectionCell : UICollectionViewCell

+ (NSString *)cellIdentifier;

@property (weak, nonatomic) id < CollectionCellDelegate > delegate;

@end

CollectionCell.m

#import "CollectionCell.h"

@implementation CollectionCell

+ (NSString *)cellIdentifier {
    return @"CollectionCell";
}


- (IBAction)buttonPressed:(UIButton *)sender {

    [self.delegate showDataFromCell:self];
}

@end

ViewController.m

#import "ViewController.h"

#import "CollectionCell.h"

@interface ViewController () <CollectionCellDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


#pragma mark - UITableView DataSource -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CollectionCell cellIdentifier] forIndexPath:indexPath];
    cell.delegate = self;
    return cell;
}

- (void)showDataFromCell:(CollectionCell *)cell {

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

    NSLog(@"Button pressed at cell with index: %ld", (long)indexPath.row);
}


@end
OMGHaveFun
  • 838
  • 1
  • 10
  • 16
  • Theis solution with the delegate works fine but there is another good way to do that. [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; Both doing their job. – ViceBrot Feb 23 '17 at 12:35