1

我设置的代表遇到了一个奇怪的问题。我得到的错误是“找不到'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;
}
4

2 回答 2

1

您应该在 SearchView.h 中有委托协议声明(我假设在您的情况下是 SearchViewDelegate)。这是委托模式的标准方式。

于 2013-10-18T18:09:27.880 回答
0

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

#import "TaskListViewController.h"

到标题。

于 2013-10-18T17:58:19.833 回答