objective-c - "Incompatible pointer..."将委托(delegate)分配给自己时发出警告

标签 objective-c ios uitableview delegates

我有一个 UITableView 的自定义子类,其中定义了一个协议(protocol),如下所示:

#import <UIKit/UIKit.h>

@protocol CustomDelegate <NSObject>
@optional
-(NSInteger)numberOfRows;
@end

@interface CustomTV : UITableView <UITableViewDelegate, UITableViewDataSource>{  
    id<CustomDelegate> *del;
}
@property (nonatomic, assign)    id<CustomDelegate> *del;
@end

现在在其他一些类中,我实例化了这个 CustomTV 并将委托(delegate)设置为 self:
    CustomTV *tbl = [[CustomTV alloc] initWithFrame:CGRectMake(0, 0, 200, 400) style:UITableViewStylePlain];
    tbl.del = self;

为什么我在 tbl.del = self 行收到“不兼容的指针...”警告?

我确实符合 header 中的 CustomDelegate 协议(protocol)。

最佳答案

您将委托(delegate)声明为指向对象指针的指针。类型id已被声明为指向对象的指针,因此请移除星号。

@interface CustomTV : UITableView <UITableViewDelegate, UITableViewDataSource>{  
    id<CustomDelegate> del;
}
@property (nonatomic, assign)    id<CustomDelegate> del;
@end

关于objective-c - "Incompatible pointer..."将委托(delegate)分配给自己时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638173/

相关文章:

objective-c - 打印Webview的页眉和页脚

objective-c - 无法在 Objective C 中调用 swift 函数。获取 NSObject 的错误接口(interface) declaration

ios - 如何修剪 UIImageView 中的透明空间?

ios - UICollectionView - 如何在同一部分显示不同高度的项目?

iphone - UITableViewCell - 设置值 : forUndefinedKey:

ios - 在 Cordova/Ionic/iOS 中滚动时的空白区域

objective-c - 如何检查并激活其他应用程序的菜单?

objective-c - NSDateFormatter : Time displayed with backslashes 问题

iphone - 如何在同一个 iPhone View 上拥有表格和其他元素?

uitableview - 意外的表重新加载/刷新有些延迟