[英]Cant pass variable from view to another view
i have a view that contains a WebView the idea is when i click on a link in that webview and the link is not from the same domain of the url i use, it will open another view that contains another webview and pass that url to it. 我有一个包含WebView的视图,这个想法是,当我单击该WebView中的链接并且该链接不是来自所使用的url的同一域时,它将打开另一个包含另一个Webview的视图并将该URL传递给它。
here my first view 这是我的第一个观点
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
var url = "http://someurl"
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(webView : UIWebView) {
}
func webView(webView: UIWebView, shouldStartLoadWithRequest r: NSURLRequest, navigationType nt: UIWebViewNavigationType) -> Bool {
var url = webView.request?.URL.absoluteString
if nt == UIWebViewNavigationType.LinkClicked {
self.switchScreen(r)
return false
}
return true
}
func switchScreen(url: NSURLRequest) {
let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("innerBrowser") as UIViewController
vc.url = url
self.presentViewController(vc, animated: true, completion: nil)
}
}
and then the second view 然后第二个视图
import UIKit
class InnerBrowserController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var innerBrowserView: UIWebView!
var url:NSURLRequest?
override func viewDidLoad() {
super.viewDidLoad()
//let requestURL = NSURL(string:url!)
//let request = NSURLRequest(URL: requestURL!)
self.innerBrowserView.loadRequest(url!)
}
}
when i set the url in the second view it works just fine. 当我在第二个视图中设置URL时,它工作得很好。
Few things come across my mind. 我几乎没有想到。
First, you cast to UIViewController
, instead of casting to InnerBrowserController
. 首先,您强制转换为UIViewController
,而不是强制转换为InnerBrowserController
。 You could do it like : 你可以这样做:
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("innerBrowser") as InnerBrowserController
The second thing is that it seems you never assign your vc.url
, hence, you don't pass the value to the second view. 第二件事是,似乎您从未分配vc.url
,因此,您没有将值传递给第二个视图。 You can do that like: 您可以这样做:
let requestURL = NSURL(string:url)
vc.url = NSURLRequest(URL: requestURL!)
The former will work only if you cast your view controller loaded from storyboard into the innerBrowserView
. 前者仅在将从情节innerBrowserView
加载的视图控制器转换为innerBrowserView
。
Maybe I missed something, but I think this will fix your problem. 也许我错过了一些事情,但是我认为这可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.