당신은 위임
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
사용할 수 있습니다 당신은 당신의 didSelectItemAtIndexPath 즉, 원하는 위치에서
[self presentAlert]
메소드를 호출
#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
대리인과 Theis 솔루션은 잘 작동하지만 그렇게 할 수있는 또 다른 좋은 방법이 있습니다. [self.window.rootViewController presentViewController : 경고 애니메이션 : 예 완료 : 없음]; 둘 다 일하고 있습니다. – ViceBrot