UISearchBar与单独的委托

时间:2014-07-14 12:50:12

标签: ios objective-c uisearchbar

我是iOS编程的新手,我需要你的帮助。 我在我们的应用程序中使用UIsearchbar但我想分开它的委托。 我希望看到每次输入时我声明的Nslog但它返回错误:( 这是我的代码。

// HomeController.h

#import <UIKit/UIKit.h>
#import "AsyncImageView.h"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
@interface HomeController : UIViewController< UIScrollViewDelegate>
@end

// HomeController.m

SearchBarDelegate *searchDelegate = [[SearchBarDelegate alloc]init];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];

searchBar.placeholder = @"Search here";
searchBar.layer.zPosition = 1000;
[self.view addSubview:searchBar];
searchBar setDelegate:searchDelegate];

// SearchBarDelegate.h

#import <UIKit/UIKit.h>

@interface SearchBarDelegate : UISearchBar<UISearchBarDelegate>

@end

// SearchBarDelegate.m

#import "SearchBarDelegate.h"

@implementation SearchBarDelegate

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBars //Clicked the box
{
    searchBars.showsCancelButton = YES;

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBars //hides searchBar and keyboard
{
    searchBars.showsCancelButton = NO;
    [searchBars resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBars  //search button in keyboard
{
    [searchBars resignFirstResponder];
}


- (void) searchBar:(UISearchBar*)searchBars textDidChange:(NSString*)searchText //while typing
{
    NSLog(@"Typing... -> %@",searchBars.text);
}


@end

0 个答案:

没有答案