1

在 .h 文件中

@interface MenuViewController : UIViewController<UIAlertViewDelegate>

在 .m 文件中

@interface TACDIYMenuViewController ()

@end

@implementation TACDIYMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.imageViews count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size = CGSizeMake(200, 100);
    return size;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuViewCellIdentifier" forIndexPath:indexPath];
    if ([indexPath row] < [self.imageViews count]) {
        cell.thumbnails.image = [self.imageViews objectAtIndex:[indexPath row]];
    } else {
        cell.thumbnails.image = [UIImage imageNamed:@"addMark.png"];
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self doSomething];
}

- (void)doSomething{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                    message:@"Are you sure"
                                                   delegate:self
                                          cancelButtonTitle:@"NO"
                                          otherButtonTitles:@"YES",nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        NSLog(@"0");
    } else {
        NSLog(@"1");
    }
}

@end

但是,clickedButtonAtIndex 没有被调用。它是怎么发生的?
顺便说一下,MenuViewController 中的视图是另一个类的子视图,它也有一个 UIAlertView 和 clickedButtonAtIndex 方法。但它运作良好。

4

3 回答 3

6

如果在单击 AlertView 上的按钮之前解除分配委托对象(在您的情况下为 TACDIYMenuViewController 的实例),则可能会发生这种情况。

于 2015-09-11T01:36:59.277 回答
1

代替

@interface MenuViewController : UIViewController<UIAlertViewDelegate>

@interface TACDIYMenuViewController : UIViewController<UIAlertViewDelegate>

然后它应该工作。

于 2013-04-25T12:27:18.253 回答
-4

请在 [alert show] 之前添加此行;alert.delegate = 自我;

于 2013-04-25T11:47:43.907 回答