以下是View Controller的代码,其中添加了搜索栏并执行操作:
SearchViewController.h
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *search;
@end
SearchViewController.m
#import "SearchViewController.h"
#import "MMDrawerController/UIViewController+MMDrawerController.h"
@interface SearchViewController ()
@end
@implementation SearchViewController
@synthesize search;
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[search.delegate self];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[search setShowsCancelButton:YES animated:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITextFieldMethod
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[search resignFirstResponder];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[search resignFirstResponder];
return YES;
}
#pragma mark - HandlingKeyboard
-(void) dismissKeyboard
{
[search resignFirstResponder];
}
#pragma mark - UISearchBarMethods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[search resignFirstResponder];
[search endEditing:YES];
[self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[search resignFirstResponder];
[self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[search resignFirstResponder];
}
@end
在搜索栏中单击以输入文本时,出现以下错误:
-[SearchViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x8e68990
最佳答案
从错误看来您的应用程序也尝试查找表视图委托方法。几张支票
您可以删除以下代码吗?
“ MMDrawerController / UIViewController + MMDrawerController.h”并尝试?
您的nib文件是否包含表格视图?如果可以,请检查是否
是否连接到任何IBOutlets?