当我在集合视图中单击按钮时,我无法将值传递给下一个视图控制器

时间:2014-02-17 09:11:59

标签: ios objective-c cocoa-touch collectionview

我遇到了一个问题。我用集合视图设置更多按钮。我需要单击按钮并将索引值传递给“NextViewController”。但是当我点击按钮时。它显示在下面的错误消息。

我试着找出哪里出错了。但我找不到。

有人可以给我一些提示吗?

非常感谢你。

========== 错误消息 ===========

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [UIViewController   itemsArray:]:无法识别的选择器发送到实例0x9e78220'

===================================

=========== ListViewController.m ==========

@interface ListViewController ()
{
    NSMutableArray * itemsArray ;

}
end
...
...  //itemsArray had some data from webesrvice
...
-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView     cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

CustomizedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomizedCell" forIndexPath:indexPath];
 NSInteger targetIndex = indexPath.row + indexPath.section*3;

if( targetIndex < itemsArray.count )
{

        [cell.cateBtn setTitle:[[itemsArray objectAtIndex:targetIndex] itemName] forState:UIControlStateNormal];

            cell.cateBtn.tag = targetIndex;
            [cell.cateBtn addTarget:self action:@selector(jumpToNextView:) forControlEvents:UIControlEventAllEvents];
            return cell;
}

-(void)jumpToNextView:(UIButton*)sender
{

    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    NextViewController *nextViewController =        
          segue.destinationViewController;
       nextViewController. itemsArray = itemsArray;
}

-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{


    if( targetIndex < itemsArray.count )
         return YES;
     else
         return NO;
}

========== NextViewController.h =========

@interface NextViewController : UIViewController

@property (strong, nonatomic) NSMutableArray *itemsArray;
@end

2 个答案:

答案 0 :(得分:1)

我猜问题是您的按钮操作方法名称:jumpToNextView 在这一行:

        [cell.cateBtn addTarget:self action:@selector(jumpToNextView:) forControlEvents:UIControlEventAllEvents];

而是在选择器或更改方法名称中更改jumpToDrScheduleList:

-(void)jumpToNextView:(UIButton*)sender
{

    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
}

答案 1 :(得分:0)

检查NextViewControllerstoryboard的班级名称。它应该是NextViewController。但是作为

  

我可以在崩溃日志中看到类名是UIViewController&amp; UIViewController没有像itemsArray那样的东西。

这就是你在这里崩溃的原因。

修改此

 [cell.cateBtn addTarget:self action:@selector(jumpToNextView:) forControlEvents:UIControlEventAllEvents];

 [cell.cateBtn addTarget:self action:@selector(jumpToDrScheduleList:) forControlEvents:UIControlEventTouchUpInside];

按钮选择器&amp;事件到UIControlEventTouchUpInside;