我有两个错误:'UIWebview'没有可见的@interface

时间:2012-12-01 14:27:39

标签: objective-c ios uiwebview

我有两个错误:'UIWebView'没有可见的@interface声明选择器'highlightAllOccurencesOfString:'

另一个:'UIWebView'没有可见的@interface声明选择器'removeAllHighlights' 请有人帮助我。

WBSecondViewController.h

 #import <UIKit/UIKit.h>
 @interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>{
}

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property(copy) NSArray *menuItems;

@property (weak, nonatomic) IBOutlet UIToolbar *webToolBar;
- (IBAction)back:(id)sender;
- (IBAction)foward:(id)sender;

-(IBAction)searchButtonPressed:(id)sender;
-(IBAction)clearHighlights:(id)sender;
@end

WBSecondViewController.m

#import "WBSecondViewController.h"
#import "Word.h"
#import "WordController.h"
#import "AddWordController.h"
#import "WBAppDelegate.h"
#import "WBFirstViewController.h"
#import "SearchWebView.h"

@interface WBSecondViewController ()

@end

@implementation WBSecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"Second", @"Second");
    self.tabBarItem.image = [UIImage imageNamed:@"second"];
}
return self;
}

- (void)viewDidLoad
{
UIMenuController *menu = [UIMenuController sharedMenuController];

[super viewDidLoad];
NSURL *theURL = [NSURL URLWithString:@"http://www.google.co.jp"];
[_webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}
-(IBAction)searchButtonPressed:(id)sender{

[_webView highlightAllOccurencesOfString:@"cat"];
}

-(IBAction)clearHighlights:(id)sender{

[_webView removeAllHighlights];

}

SearchWebView.h

#import <Foundation/Foundation.h>

@interface SearchWebView : UIWebView

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;

@end

SearchWebView.m

#import "SearchWebView.h"

@implementation SearchWebView

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];

NSString *startSearch = [NSString     stringWithFormat:@"uiWebview_HighlightAllOccurencesOfString('%@')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];

NSString *result = [self stringByEvaluatingJavaScriptFromString:@"uiWebview_SearchResultCount"];
return [result integerValue];
}

- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];
}

@end

1 个答案:

答案 0 :(得分:2)

U已经将uiwebview子类化并称之为SearchWebView,但是当您在wbsecondviewcontroller中创建Web视图实例时,您使用常规Web视图而不是您创建的子类,而常规Web视图没有两个额外的方法您为该自定义项定义的。在wbsecondviewcontroller.h中的@interface上面做@class SearchWebView。然后在声明属性UiWebView的地方,将其声明为SearchWebView。在wbsecondviewcontroller的.m文件中执行#import "SearchWebView.h"