我有一个带有UIWebView控制器的视图控制器。我正在尝试在Web视图的html内容中获取javascript以将一些信息传递给我的目标c代码。我在网上遇到了一些在javascript函数中设置window.location的示例,然后通过将视图控制器设置为Web视图的委托并在shouldStartLoadWithRequest函数中捕获它来捕获生成的事件。不幸的是我无法让它工作,因为对我来说,即使我正在设置Web视图的委托,也不会调用shouldStartLoadWithRequest。
我的代码如下:
界面:
@interface StoryTextViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *storyWebView;
NSString *information;
}
@property (nonatomic, retain) IBOutlet UIWebView *storyWebView;
@property (nonatomic, retain) NSString *information;
@end
在界面中,* information变量由调用StoryTextViewController的上一个视图设置。
实施:
#import "StoryTextViewController.h"
@implementation StoryTextViewController
@synthesize storyWebView;
@synthesize information;
- (NSString *) contructHTMLText
{
NSString *HTMLText = @"";
NSArray *words = [information componentsSeparatedByString:@" "];
NSString *header =
@"<html>\n"
"<head>\n"
"<script type=\"text/javascript\">\n"
"function marktext(theSpanID)\n"
"{\n"
" var theSpan=document.getElementById(theSpanID);\n"
" if (theSpan.className == \"noHilite\")\n"
" {\n"
" theSpan.className = \"hilite\";\n"
" window.location = \"true\";\n"
" }\n"
" else\n"
" {\n"
" theSpan.className = \"noHilite\";\n"
" window.location = \"false\";\n"
" }\n"
"}\n"
"</script>\n"
"<style type=\"text/css\">\n"
".hilite\n"
"{\n"
" background-color:red;\n"
" color:white;\n"
" font: bold 60px arial,sans-serif\n"
"}\n"
".noHilite\n"
"{\n"
" background-color:white;\n"
" color:black;\n"
" font: 60px arial,sans-serif\n"
"}\n"
"</style>\n"
"</head>\n"
"<body>\n"
"<div class=\"storyText\">\n";
NSString *tailer =
@"</div>\n"
"</body>\n"
"</html>\n";
HTMLText = [HTMLText stringByAppendingString:header];
for (NSInteger i = 0; i < [words count]; i ++)
{
NSString *tag = [NSString stringWithFormat:@" <span id=\"mytext%d\" class=\"noHilite\" onclick=\"marktext(\'mytext%d\')\">%@</span> \n", i, i, (NSString *)[words objectAtIndex:i]];
HTMLText = [HTMLText stringByAppendingString:tag];
}
HTMLText = [HTMLText stringByAppendingString:tailer];
NSLog(HTMLText);
return HTMLText;
}
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
storyWebView.delegate = self;
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *HTMLData = [self contructHTMLText];
[storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// handle event here
return true;
}
@end
发生的事情是,基本上,constructHTML函数接受* information变量中的文本并用文本中的每个单词包含一些html和javascript,这样无论何时单击一个单词,都会在其上应用css高亮度。我想要做的是计算高亮词的数量。我这样做是通过尝试传递函数,只要单击一个单词就调用该函数,但就像我说的那样,应该触发的shouldStartLoadWithRequest方法永远不会被执行。
我见过很多人做过这类事情,但我似乎无法弄清楚为什么它没有为我而战
答案 0 :(得分:4)
您可以在设置委托
上设置断点if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
storyWebView.delegate = self;
}
我很确定storyWebView在那一刻是零。
您可以通过以下方式解决问题:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *HTMLData = [self contructHTMLText];
// empty statement
// the trick is that if the view is not loaded from the .nib yet
// it will be loaded and all connections established
if (self.view);
// here is setting delegate
storyWebView.delegate = self;
[storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]];
}
答案 1 :(得分:1)
您应该在viewDidLoad
中设置委托,而不是initWithNibName:bundle:
。或者,打开您的nib文件并从Web视图控制拖动到文件所有者,然后从下拉列表中选择“委托”。
答案 2 :(得分:0)
window.location
不应该是您要访问的目标网址的值吗?在marktext
功能中,您将其设置为true
或false
。只有在收到URL请求并且即将到达某个地方时才会调用shouldStartLoadWithRequest
。不确定将位置设置为布尔值是否足以触发导航事件。您可以将其设置为魔术URL,然后从shouldStartLoadWithRequest
例程返回FALSE,如果它要求导航到该特定魔术URL并阻止它实际到达某个地方。然后可以允许有效的URL通过。
也可能想看看对this SO question的回复。 window.location
可能存在一些时间问题。
答案 3 :(得分:0)
我也看到了这个问题,因为我已经设置了
articleWeb.userInteractionEnabled = NO;`
你最好检查你的代码,设置
articleWeb.userInteractionEnabled = YES;`