ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-iPhone应用程序中未调用shouldStartLoadWithRequest

2019-10-24 12:34:14  阅读:293  来源: 互联网

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


我有一个带有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>&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方法.

我已经看到很多人都在做这种事情,但是我似乎无法弄清楚为什么它不适合我

解决方法:

您可以在设置委托时设置断点

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

标签:objective-c,event-handling,cocoa-touch,javascript,iphone
来源: https://codeday.me/bug/20191024/1920763.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有