我设置的代表遇到了一个奇怪的问题。我得到的错误是“找不到'SearchViewDelegate'的协议声明
ListViewController.h
#import "SearchView.h"
@class SearchView;
@protocol SearchViewDelegate <NSObject>
@optional
- (void)didTapSearchButton:(SearchView *)searchView;
@end
@interface TaskListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate>{
SearchView *searchView;
}
@property(nonatomic, assign) id<SearchViewDelegate> delegate;
列表视图控制器.m
- (IBAction)didTapSearchButton
{
NSString *searchTerm = searchView.searchField.text;
if ([searchTerm isEqualToString:@""]) {
[self.view endEditing:YES];
return ;
}
[searchView resignFirstResponder];
NSArray *results = [[CoreDataManager sharedInstance] fetchTaskByName:searchTerm];
[tasks removeAllObjects];
[tasks addObjectsFromArray:results];
[self.view endEditing:YES];
[taskTable reloadData];
}
搜索视图.h
#import <UIKit/UIKit.h>
@interface SearchView : UIView <UITextFieldDelegate, UISearchBarDelegate, SearchViewDelegate> // <- where the protocol error occurrs
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@end
我以为我正确地宣布了代表和一切。
这是我想在 searchView.m 中调用委托方法的地方:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.searchField.delegate didTapSearchButton:self];
[textField resignFirstResponder];
return YES;
}