0

Within a UIView how do I cast self.nextResponder (which points to the view's controller) to avoid a compiler warning on the third line below?

UITableView* tableView = [ [ UITableView alloc ] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.tag = TAG_IMAGEADMIN_IMAGESLISTVIEW;
tableView.delegate = self.nextResponder; // compiler warning here
tableView.dataSource = self;
[self addSubview:tableView];

Thanks

whatdoesitallmean
  • 1,586
  • 3
  • 18
  • 40
  • possible duplicate of [Cast an instance of a class to a @protocol in Objective-C](http://stackoverflow.com/questions/617616/cast-an-instance-of-a-class-to-a-protocol-in-objective-c) – jrturton Jun 11 '12 at 15:03

2 Answers2

0

You can cast to a protocol as follows:

tableView.delegate = (id<UITableViewDelegate>)self.nextResponder
jrturton
  • 118,105
  • 32
  • 252
  • 268
0

As UITableViewDelegate is a protocol, you can cast as

tableView.delegate = (id < UITableViewDelegate >)self.nextResponder. This tells the compiler that self.nextResponder is any object that confirms to UITableViewDelegate protocol.

If you know that the nextResponder is of any perticular class lets say UIViewController, you can write as

tableView.delegate = (UIViewController < UITableViewDelegate>*)self.nextResponder. This tells compiler that nextResponder is of type UIViewController class that confirms to UITableViewDelegate protocol.

nkongara
  • 1,233
  • 8
  • 14
  • Complete discussion at http://stackoverflow.com/questions/617616/cast-an-instance-of-a-class-to-a-protocol-in-objective-c – nkongara Jun 11 '12 at 15:20