我正在尝试从xWebAppJS类中的callbackHandler()调用loadAddressURL()到Webview中。我从ViewController调用callbackHandler()没有问题,但反之则不成立。
我基本上是尝试从另一个类重新加载Webview中的页面或任何其他页面。
提前致谢!对不起新手问题。
class ViewController: UIViewController, UIWebViewDelegate{
//... viewDidLoad()
//... viewDidAppear()
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let scheme = String(request.URL!)
NSLog(scheme)
//checks string if contains sequence
if scheme.containsString("ios:JS.") {
xWebAppJS.callbackHandler(scheme)
return false
}
NSLog("Link was not tapped")
return true
}
func loadAddressURL(URL: String) {
let requestURL = NSURL(string: URL)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}
这是包含callbackHandler
的另一个类class xWebAppJS: ViewController{
class func callbackHandler(oScheme: String) {
// I want to call the loadAddressURL() out of this function
}
}
答案 0 :(得分:0)
由于xWebAppJS符合ViewController,您只需执行xwebAppJS().loadAddressUrl(Url:String)
答案 1 :(得分:0)
This will be the answer !
**inside the class**
let requestURL = NSURL(string: "string URL")
self.loadAddressUrl(requestURL)
**outside the class**
let xWebAppJS = xWebAppJS()
let requestURL = NSURL(string: "string URL")
xWebAppJS.loadAddressUrl(requestURL)
func loadAddressURL(requestURL: NSURL) {
let request = NSURLRequest(URL: requestURL)
webView.loadRequest(request)
}