【问题标题】:Cast UIView nextResponder to UITableView delegate将 UIView nextResponder 转换为 UITableView 委托
【发布时间】:2012-06-11 14:56:20
【问题描述】:
如何在 UIView 中强制转换 self.nextResponder(指向视图的控制器)以避免下面第三行出现编译器警告?
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];
谢谢
【问题讨论】:
标签:
objective-c
uiview
uiviewcontroller
casting
delegates
【解决方案1】:
您可以按如下方式转换为协议:
tableView.delegate = (id<UITableViewDelegate>)self.nextResponder
【解决方案2】:
由于 UITableViewDelegate 是一个协议,你可以转换为
tableView.delegate = (id )self.nextResponder。
这告诉编译器 self.nextResponder 是任何确认 UITableViewDelegate 协议的对象。
如果你知道 nextResponder 属于任何特定的类,比如 UIViewController,你可以写成
tableView.delegate = (UIViewController *)self.nextResponder。
这告诉编译器 nextResponder 的类型是 UIViewController 类,该类确认 UITableViewDelegate 协议。