2017-02-22 3 views
1

UICollectionView의 Cell에서 UIAlertController를 보여주고 싶습니다.UICollectionViewCell의 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]; 

문제는 세포가없는 점이다 [자체를 presentViewController : 경고 애니메이션 : YES 완료 : 닐]; 방법.

누군가 나를 도울 수 있습니까?

답변

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

사용할 수 있습니다 당신은 당신의 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 
+0

대리인과 Theis 솔루션은 잘 작동하지만 그렇게 할 수있는 또 다른 좋은 방법이 있습니다. [self.window.rootViewController presentViewController : 경고 애니메이션 : 예 완료 : 없음]; 둘 다 일하고 있습니다. – ViceBrot

1

콜렉션 뷰를 포함하는 뷰 컨트롤러에서 메소드를 생성하고 셀에서 메소드를 호출 할 수 있습니다. 이런 식으로 뭔가 : 다음

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

그리고는

+0

더 잘 트리거 각 셀에 대한 경고 컨트롤러의 새 복사본을 인스턴스화하려고보다,이 작업을 수행하는. – brandonscript