user2955041
user2955041

Reputation: 13

SearchBar text only working viewDidLoad

I have UIWebView for displays content. I need to search some text inside webview. I used UISearchBar. But the code is not searching and highlighting.

below code is not working:

.h file:

@interface detailsArtViewController : UIViewController<UISearchBarDelegate>
{

    UISearchBar *searchbar;

}

.m file:

-(void)viewDidLoad
{
   searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];


searchbar.delegate=self;

[wbCont addSubview:searchbar];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{
     NSLog(@"search is %@",searchBar.text);

[self highlightAllOccurencesOfString:searchBar.text];
}

Upvotes: 0

Views: 475

Answers (2)

Vinodh
Vinodh

Reputation: 5268

Have you set delegate for UISearchBar

In your reference link itself they set delegate for UISearchBar

@interface ViewController : UIViewController <UISearchBarDelegate>
{
 UISearchBar *mysearchbar;
}

They are setting the delegate in XIB . If you are not using XIB . the set

delegate like mysearchbar.delegate =self;

then only delegate methods of UISearchBar like ,

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 

will be called

Please change .m file code as follows

-(void)viewDidLoad
{
  [super viewDidLoad];
    searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchbar.delegate=self;
    [wbCont addSubview:searchbar];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{
      //  NSString *search=[NSString stringWithFormat:@"Code"];
      NSString *search=[NSString stringWithFormat:@"%@",searchBar.text];
      NSLog(@"search is %@",search);
      [self highlightAllOccurencesOfString:search];
}

Upvotes: 1

MCKapur
MCKapur

Reputation: 9157

The correct method would be

- (void)viewDidLoad 

not

- (void)ViewDidLoad

And you should include this in your -viewDidLoad method:

[super viewDidLoad];

I'm not exactly sure what you mean by "not working", but in the first method you draw the search bar to the view, where in the second method.... you don't.

Upvotes: 1

Related Questions