【问题标题】:Event on html alert in xcodexcode中的html警报事件
【发布时间】:2012-11-27 23:00:45
【问题描述】:

我有一个 HTML 文件,可以用我的 xcode 项目计算各种电缆尺寸(其余的都在目标 c 中),如果电缆超出范围,它会触发警报 alert ("No suitable cables"); 在此我想触发一个动作.我可以告诉我的应用程序在 HTML 中发生了事件吗?我听说过listners,认为我可能需要在这种情况下使用?不知道我会如何实现它的想法

【问题讨论】:

标签: html objective-c ios xcode events


【解决方案1】:

您可以通过在 webview 中触发一个 URL 来完成此操作,该 URL 将加载自定义前缀,如果需要发送电缆警报,您可以在 javascript 中的 webview 的 shouldStartLoadWithRequest 方法中查找该前缀。

ex. sendCableAlert://scenario1.com

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *alertPrefix = @"sendCableAlert://";

    if ([[[request URL] absoluteString] hasPrefix:alertPrefix]) {
        //You've hit an alert do something..
        NSLog(@"CABLE ALERT!");

        return NO;
    }
    else {
        return YES;
    }
}

【讨论】:

  • 我想我理解了代码的第二部分和这背后的原理,但我仍然没有完全理解。 sendCableAlert://scenario1.com,是在我的 html 文件中吗?
  • 是的,您将拥有一个 javascript 触发器,我猜它会根据您的计算结果有条件地发送,类似于 loadUrl(somesite.com)。
  • 我想这就是我困惑的地方,加载网址?我没有我的 html 代码,它是本地 html 文件中的计算器(抱歉应该说明)` ...` form.percentage_drop.value = Math.round(mycable.percentage_drop(I,l,V) * 10)/10; form.current.value = Math.round(I*100)/100; } else { alert("没有合适的电缆"); } }
  • 试试这个然后 form.percentage_drop.value = Math.round(mycable.percentage_drop(I,l,V) * 10 )/10; form.current.value = Math.round(I*100)/100; } else { loadURL(sendCableAlert://testingprefix); } } // 我刚刚从快速谷歌搜索 loadURL javascript 中得到了 js。
  • 这样你现在正在做你的 js 警报,它只是使用 javascript 来尝试加载一个 URL,obj-c 将看到它不是一个真正的 URL,而只是一种传递消息的方式到视图控制器
猜你喜欢
  • 2016-02-03
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
相关资源
最近更新 更多