【问题标题】:UIAlertController from UICollectionViewCell [duplicate]来自 UICollectionViewCell 的 UIAlertController [重复]
【发布时间】:2017-02-22 16:10:40
【问题描述】:

我想从 UICollectionView 的单元格中显示一个 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];方法。

也许有人可以帮助我?

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案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];
    }
    

    然后从你喜欢的任何地方调用 [self presentAlert] 方法,即你的 didSelectItemAtIndexPath

    【讨论】:

    • 这样做肯定比尝试为触发它的每个单元格实例化警报控制器的新副本更好。
    【解决方案2】:

    你可以使用委托

    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
    

    【讨论】:

    • 与委托的解决方案工作正常,但还有另一种好方法可以做到这一点。 [self.window.rootViewController presentViewController:alert animated:YES completion:nil];两人各司其职。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 2020-08-25
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多