0

I'm using prepareForSeque:sender: to dynamically set the UITableView's delegate and dataSource properties before it gets pushed onto the navigation stack. The delegate methods are never called and Xcode returns the error below. Unfortunately, Xcode doesn't give me much of a stack trace and the only indication of what object is being used as the data source is an instance of UIViewControllerWrapperView which I think may be something generated by the storyboard library. Any ideas? Is this not even possible?

The error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewControllerWrapperView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7f49370'

Here's my code:

/**
 * Handle the various segues
 *
 * @author Jesse Bunch
 **/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the selected cell
    NSIndexPath *selectedIndexPath = self.tableView.indexPathForSelectedRow;
    ReportTableViewCell *cell = (ReportTableViewCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];

    // Instantiate the selected report's context
    // TODO: Insert error handling here
    Class reportClass = NSClassFromString([cell.reportInfo objectForKey:@"ReportClass"]);
    BaseReportContext *reportContext = [[reportClass alloc] init];
    reportContext.delegate = self;

    // Get the destination options controller
    ReportOptionsTableViewController *optionsController = (ReportOptionsTableViewController *)segue.destinationViewController;

    // Let the report context be the delegate of the options controller
    // The report context should contain all the information needed
    // to display the necessary report customization options
    optionsController.tableView.delegate = reportContext;
    optionsController.tableView.dataSource = reportContext;


}
Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59
  • will you please make a simple isKindOfClass:[BaseReportContext class] check and log [cell.reportInfo objectForKey:@"ReportClass"] for the negative results ? – A-Live Jan 08 '12 at 23:59
  • I added a screenshot from the console to show the types are as expected... – Jesse Bunch Jan 09 '12 at 00:04
  • Does BaseReportContext implement the UITableViewDataSource protocol? – jonkroll Jan 09 '12 at 00:19
  • You can get a stack trace for the exception by setting an exception breakpoint. http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 – rob mayoff Jan 09 '12 at 00:20
  • @Jesse Bunch, thanks, that looks good. Will you then try to retain the delegate/ds objects in a more reliable way (say keeping it at var/property array/dictionary) ? i believe delegate is not supposed to be retained by the object, it might happen that all the reportContext are being released. at least the log perfectly matches the case of memory management issue, i believe you noticed it yourself. – A-Live Jan 09 '12 at 00:30
  • @jonkroll It does, and I have breakpoints on that method as well as the subclass which is what is actually instantiated. They're never called. – Jesse Bunch Jan 09 '12 at 00:31
  • @user792677, that worked! Will you make your comment an answer? – Jesse Bunch Jan 09 '12 at 00:35
  • @Jesse Bunch, glad that helped. i believe you'll have to make a solid reference for delegates regardless of whether you are going to use ARC as there must be a good reason of why delegates are not retained by objects. If you don't like it, you can always keep a retained BaseReportContext reference at ReportOptionsTableViewController. – A-Live Jan 09 '12 at 00:44
  • The latter is what I decided to do. It will make the most sense as I further flesh out that functionality. – Jesse Bunch Jan 09 '12 at 00:45

1 Answers1

2

Will you then try to retain the delegate/ds objects in a more reliable way (say keeping it at var/property array/dictionary) ? i believe delegate is not supposed to be retained by the object, it might happen that all the reportContext are being released. at least the log perfectly matches the case of memory management issue, i believe you noticed it yourself.

A-Live
  • 8,904
  • 2
  • 39
  • 74