2017-02-22 30 views
1

我想從UICollectionView的Cell顯示一個UIAlertController。UIAollectionViewCell的UIAlertController

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

問題是,單元格沒有[self presentViewController:alert animated:YES completion:nil];方法。

也許有人可以幫助我嗎?

回答

0

您可以使用委託

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

Theis解決方案與委託工作正常,但有另一種好辦法做到這一點。 [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; 雙方都在做他們的工作。 – ViceBrot

1

您可以在viewController中創建一個包含您的集合視圖的方法,並調用您的單元格中的方法。事情是這樣的:

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

然後調用從你喜歡的地方,即你的didSelectItemAtIndexPath的[self presentAlert]方法

+0

肯定更好地做到這一點,比嘗試實例警報控制器的新副本觸發它的每個細胞。 – brandonscript