2017-02-22 5 views
1

Ich möchte einen UIAlertController aus der Zelle einer UICollectionView zeigen.UIAlertController von UICollectionViewCell

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

Das Problem ist, dass die Zelle nicht hat [self presentViewController: alert animierte: JA Abschluss: nil]; Methode.

Vielleicht kann mir jemand helfen?

Antwort

0

Sie delegieren

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 können

#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 
+0

Theis Lösung mit dem Delegierten funktioniert gut, aber es gibt einen anderen guten Weg, das zu tun. [self.window.rootViewController presentViewController: Warnung animiert: YES completion: nil]; Beide machen ihre Arbeit. – ViceBrot

1

Sie können in Ihrer ViewController eine Methode erstellen, die Ihre Sammlungsansicht enthält, und die Methode aus Ihrer Zelle aufrufen. Etwas wie folgt aus:

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

Und dann rufen Sie die [self presentAlert] Methode aus, wo immer Sie wollen also Ihre didSelectItemAtIndexPath

+0

Auf jeden Fall besser, dies zu tun, als versuchen, eine neue Kopie des Alarm-Controller für jede Zelle zu instanziieren, die es auslöst. – brandonscript

Verwandte Themen