Question

I had encounter a question. I used collection view set more button. I need click button and passs index value to "NextViewController". But when I click the button. It is show below error message.

I try to find where's error. But I can't find.

Have any one can give me some hint?

thank you very much.

==========error message ===========

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UIViewController itemsArray:]: unrecognized selector sent to instance 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
Was it helpful?

Solution 2

check the class name for NextViewController in storyboard. it should be NextViewController. but as

i can see in crash log class name is UIViewController & UIViewController has nothing like itemsArray.

thats the reason you are getting crash here.

modify this

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

with

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

selector for button & event to UIControlEventTouchUpInside;

OTHER TIPS

I guess the Problem is with your button Action method name: jumpToNextView in this line:

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

Rather Change jumpToDrScheduleList: in selector or Change Method name.

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

    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top