javascript - shouldStartLoadWithRequest 未在我的 iPhone 应用程序中调用

标签 javascript iphone objective-c cocoa-touch event-handling

我有一个带有 UIWebView Controller 的 View Controller 。我试图在 web View 的 html 内容中获取 javascript,以将一些信息传递给我的 objective-c 代码。我在网上遇到了一些在 javascript 函数中设置 window.location 的示例,然后捕获通过将 View Controller 设置为 Web View 的委托(delegate)并在 shouldStartLoadWithRequest 函数中捕获它而生成的事件。不幸的是我无法让它工作,因为对我来说 shouldStartLoadWithRequest 从未被调用,即使我正在设置 web View 的委托(delegate)。

我的代码如下:

界面:

@interface StoryTextViewController : UIViewController <UIWebViewDelegate>
{
    IBOutlet UIWebView *storyWebView;   
    NSString *information;
}

@property (nonatomic, retain) IBOutlet UIWebView *storyWebView;
@property (nonatomic, retain) NSString *information;

@end

在界面中,*information 变量由调用 StoryTextViewController 的前一个 View 设置。

实现:

#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>&nbsp;\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 方法。

我见过很多人做这种事,但我似乎无法弄清楚为什么它不适合我

最佳答案

您可以在设置委托(delegate)上放置断点

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    storyWebView.delegate = self;
}

我很确定那一刻 storyWebView 为 nil。

您可以通过这种方式修复它:

- (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: @""]];  
}

关于javascript - shouldStartLoadWithRequest 未在我的 iPhone 应用程序中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1076609/

相关文章:

javascript - 如何使用 JS 动画文本循环

javascript - 在像 Object 这样的 Array 上使用 slice() 方法

javascript - 滚动到 Javascript 中的文本位置

iphone - 有没有办法使用 xcode/iphone sim 模拟多个 iphone?

java - 在 ObjC 中检索 String 的字节会返回与 Java 不同的结果

Javascript (ES6) 日期到字符串格式

iphone - 我应该为 App Store 的每个应用程序创建一个唯一的 Apple App ID 吗?

iphone - 替换 UINavigationController 层次结构中的 UIViewController

iphone - 捕获UIView中的所有触摸事件?

ios - 仅加载UITableView中的可见单元格