【问题标题】:shouldStartLoadWithRequest is not called in my iPhone App在我的 iPhone 应用程序中没有调用 shouldStartLoadWithRequest
【发布时间】:2009-07-02 20:47:36
【问题描述】:

我有一个带有 UIWebView 控制器的视图控制器。我试图在 web 视图的 html 内容中获取 javascript,以将一些信息传递给我的目标 c 代码。我在网上遇到了许多示例,它们在 javascript 函数中设置 window.location,然后通过将视图控制器设置为 Web 视图的委托并在 shouldStartLoadWithRequest 函数中捕获它来捕获生成的事件。不幸的是,我无法让它工作,因为对我来说 shouldStartLoadWithRequest 永远不会被调用,即使我正在设置 Web 视图的委托。

我的代码如下:

界面:

@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 方法永远不会执行。

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

【问题讨论】:

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


    【解决方案1】:

    你可以在设置委托上放断点

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

    【讨论】:

    • 我按照你的建议做了,但没有任何区别。无论如何,self 并不是一开始就为零的。您还有什么建议吗?
    • self 肯定不会是 nil,那 storyWebView 呢?
    • 不。故事视图也可以很好地显示 html。我完全被难住了。如果你愿意,也许我可以把我编码的东西发给你?我的老板有点不耐烦,我越来越绝望
    • 您可以发送。在 gmail dot com 上使用我的名字 dot second name
    • 好的,问题的真正原因是您正在通过 loadHTMLString 加载 HTML
    【解决方案2】:

    您应该在viewDidLoad 中设置委托,而不是在initWithNibName:bundle: 中。或者,打开您的 nib 文件并从 Web 视图控制并拖动到您的文件所有者,然后从下拉列表中选择“委托”。

    【讨论】:

      【解决方案3】:
      • window.location 不应该是您要访问的目标 URL 的值吗?在您的marktext 函数中,您将其设置为truefalseshouldStartLoadWithRequest 仅在收到 URL 请求并即将到达某个地方时才被调用。不确定将位置设置为布尔值是否足以触发导航事件。您可以将其设置为一个魔术 URL,然后从 shouldStartLoadWithRequest 例程返回 FALSE,如果它要求导航到该特定魔术 URL 并阻止它实际到达某个地方。然后可以允许有效的网址通过。

      • 还可能想看看对this SO question 的回复。 window.location 可能存在一些时间问题。

      【讨论】:

      • 我实际上是在尝试将 window.locaton 设置为字符串“true”或“false”,这并不重要,因为我的代码不适用于任何字符串。我还查看了您发送给我的链接,但这不是我需要的。他的问题是 shouldStartLoadWithRequest 对他来说效果太好了,就我而言,它根本没有被调用!
      • 不确定您为什么尝试将位置值设置为“真”或“假”?这就像在 URL 栏中输入“true”。如果您使用像 stackoverflow.com 这样的有效 URL 会发生什么?我的观点是我不认为 shouldStartLoadWithRequest 被调用,除非你给它一个有效的 URL 来跟踪。
      【解决方案4】:

      我也看到了这个问题,因为我设置了

      articleWeb.userInteractionEnabled = NO;`
      

      你最好检查你的代码,设置

      articleWeb.userInteractionEnabled = YES;`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-18
        • 2010-11-02
        • 1970-01-01
        • 2011-05-28
        相关资源
        最近更新 更多