找不到我的自定义委托的协议声明

时间:2013-10-18 17:55:54

标签: iphone ios delegates

我遇到了我设置的代理人的一个奇怪的问题。我得到的错误是“无法找到'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;

ListViewController.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];
}

SearchView.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;
}

2 个答案:

答案 0 :(得分:1)

您应该在SearchView.h中拥有委托协议声明(我假设它是您的案例中的SearchView委托)。这是委托模式的标准方式。

答案 1 :(得分:0)

如果错误发生在SearchView.h中,我认为您需要添加

#import "TaskListViewController.h"

到标题。