0

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
dickfala
  • 3,246
  • 3
  • 31
  • 52
  • Could it be that your segue.destinationViewController is not really of type NextViewController? I'm not sure what could happen if that was the case but i assume segue.destinationViewController returns id so the assignment is done even if it's not the correct type? – Peter Segerblom Feb 17 '14 at 09:19
  • Either way the problem seems to be the assignment of items array to your nextViewController. Turn on exception breakpoints to check. – Peter Segerblom Feb 17 '14 at 09:20
  • You should remove the brackets ( '{' and '}' ) in your nextViewController's header, not sure that your syntax works. – Rom. Feb 17 '14 at 09:24
  • Is the `segue.destinationViewController` a `UINavigationController` by any chance? You could add a breakpoint to find out. – JoeFryer Feb 17 '14 at 09:26
  • @JoeFryer the exception clearly states its a UIViewController. But you right he should ad a exception breakpoint. – Peter Segerblom Feb 17 '14 at 09:27
  • My bad. Note to self - read the question. – JoeFryer Feb 17 '14 at 09:28
  • 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. – Pawan Rai Feb 17 '14 at 09:36
  • and where is `jumpToNextView:` method implementation? – Mykola Denysyuk Feb 17 '14 at 09:40
  • sorry ~! I post have some mistakes. I had fix the content. thanks – dickfala Feb 18 '14 at 00:52
  • The problem is happened on "nextViewController. itemsArray = itemsArray;" If I comment this line. There are not crash. But I don't know whats happend on here . thanks. – dickfala Feb 18 '14 at 00:54
  • I had solved the problem~~ I appreciate for everyone especially @pawan. The problem is my storyboard class name not set NextViewController. Thank you very much~! – dickfala Feb 18 '14 at 01:00

2 Answers2

1

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];
}
  • looks like he just forgot to post `jumpToNextView:` implementation, cuz if add wrong selector to `cateBtn` he would get another exception – Mykola Denysyuk Feb 17 '14 at 09:41
  • yap. I'm not changed to jumpToNextView name. sorry~ – dickfala Feb 17 '14 at 11:02
  • no, I'm not solved my problem. It just method name not changed when I post the question. I am try to find answer. thanks . – dickfala Feb 17 '14 at 11:26
  • Thank you, @user203108,@ Mykola Denysyuk . I had solved my problem.The answer is I had not set class name in storyboard. – dickfala Feb 18 '14 at 01:01
0

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;

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • I had solved the problem. The problem is my storyboard class name not set NextViewController. Thank @pawan very much~! – dickfala Feb 18 '14 at 01:00