具有自我类型的协议功能

时间:2019-03-11 05:20:35

标签: ios objective-c uitableview protocols

代码如下:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol HomeHeaderCellDelegate <NSObject>
- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;
@end

@interface HomeHeaderCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIView *secondMenuRowView;
@property (weak, nonatomic) IBOutlet UIButton *moreLessMenuButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondMenuRowViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UIButton *askButton;
@property (weak, nonatomic) IBOutlet UIButton *contactButton;
@property (weak, nonatomic) IBOutlet UIButton *benchmarkButton;
@property (weak, nonatomic) IBOutlet UIButton *buySellButton;
@property (weak, nonatomic) IBOutlet UIButton *marketButton;

@property (nonatomic, weak) id <HomeHeaderCellDelegate> delegate;
@property BOOL isFullMenu;

- (void)toggleHeight;

@end

NS_ASSUME_NONNULL_END

此行有错误

- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;

它说:

  

预期类型

2 个答案:

答案 0 :(得分:2)

编译器需要知道类HomeHeaderCell是在某处声明的。

实际上,您必须使用@import语句导入该类,但是在这种情况下,您只需要类型而不需要实现详细信息。 @class指令是一个前向引用,它可以确认类型,但可以避免循环引用问题。

在导入行下添加@class,请使用现代的@import语句。

@import UIKit;
@class HomeHeaderCell;

答案 1 :(得分:0)

您可以做到

#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@protocol HomeHeaderCellDelegate;

@interface HomeHeaderCell : UITableViewCell
@property (nonatomic, weak) id <HomeHeaderCellDelegate> delegate;
@end

@protocol HomeHeaderCellDelegate <NSObject>
- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;
@end

NS_ASSUME_NONNULL_END