0

i have created one custom cell and put button on it and button event is like this

   [cell.BtnRequest addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];

and didtapbutton code is this

 - (void)didTapButton:(id)sender {

    WorkerDetailsVC *wvc=[self.storyboard instantiateViewControllerWithIdentifier:@"workerdetailsvc"];
    NSIndexPath *indepath = [self.tblorderdata indexPathForCell:sender];

    wvc.arrworkarrequest=[arrData objectAtIndex:indepath.row];

    NSLog(@" arr send :%@", wvc.arrworkarrequest);

    wvc.struserid = struserid;

   [self.navigationController pushViewController:wvc animated:YES];

    }

and on the next page i got the array arrworkarrequest and after the load all next page it will show error like this

       2014-10-01 15:08:42.607 demo[2151:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860
       2014-10-01 15:08:42.613 dmeo[2151:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860'
Yash Joshi
  • 199
  • 8
  • 17
Ujesh
  • 1,698
  • 2
  • 23
  • 35

2 Answers2

3

Your problem is that sender is of type UIButton not UITableViewCell so it's getting stuck in the [self.tblorderdata indexPathForCell:sender] because that method call expects a type of UITableViewCell.

You need to find the Cell that the button is in. You may be able to do this using a while loop (although it's a bit horrible). Something like:

id obj = sender;
while (![obj isKindOfClass:[UITableViewCell class]]) {
  obj = [sender superview];
}

[self.tblorderdata indexPathForCell:obj];

This feels like it's abusing the view hierarchy though.

James Snook
  • 4,063
  • 2
  • 18
  • 30
  • +1 - Exactly, you are adding the control event to cell.BtnRequest, so the UIButton *BtnRequest*, rather than the cell. You may be able to get away with using the BtnRequest's superview, which should be the cell - I'm not certain this will work though. – Jonathan King Oct 01 '14 at 10:19
  • please tell me more about this , what i do? – Ujesh Oct 01 '14 at 10:19
  • if i do same code without array then it's work completely, so it's issue with my large array i send 22 object with key to next page – Ujesh Oct 01 '14 at 11:16
  • If you haven't already I recommend butting a breakpoint on all exceptions this way your program should stop on the call that is triggering the issue. – James Snook Oct 01 '14 at 11:37
-2

This happens if you compare the length of a string directly

for example

NSString *myString = @"2";

using this statement will generate the error

if(myString.length==2)

  • No it won't. This error happens because the length method has been called on an NSNumber and NSNumber doesn't respond to the method length. Something like this (@2).length might give this error. As he's not calling the length method in this code it's most likely because of the type confusion of `sender`. – James Snook Oct 01 '14 at 10:19
  • Its happening after his next view controller gets called so i believe something wrong is happening there.He should probably add a break point and check where exactly he gets that exception. – MOSES AFONSO Oct 01 '14 at 10:25