0

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];
}
4

2 回答 2

1

正确的方法是

- (void)viewDidLoad 

不是

- (void)ViewDidLoad

你应该在你的-viewDidLoad方法中包含这个:

[super viewDidLoad];

我不确定您所说的“不工作”是什么意思,但是在第一种方法中,您将搜索栏绘制到视图中,而在第二种方法中......您没有。

于 2013-11-06T05:55:08.523 回答
1

你有没有为UISearchBar

在您的参考链接本身中,他们为UISearchBar

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

他们在 XIB 中设置代表。如果您不使用 XIB 。集合

代表喜欢 mysearchbar.delegate =self;

然后只委托UISearchBar like 的方法,

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 

将被调用

请更改 .m 文件代码如下

-(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];
}
于 2013-11-06T06:25:16.927 回答