简体   繁体   English

无法将变量从一个视图传递到另一个视图

[英]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.

相关问题 从另一个视图将变量传递给 SwiftUI AVPlayer - Pass variable to SwiftUI AVPlayer from another view 如何在 SwiftUI 中将变量从一个视图传递到另一个视图 - How to pass variable from one view to another in SwiftUI 从另一个视图控制器将变量直接传递给UICollectionViewCell - Pass variable directly to UICollectionViewCell from another view controller 从JSON获取价值并传递到另一个视图 - Get value from JSON and pass to another view 将数据从一个视图传递到另一个视图 - Pass data from one view to another 将NSManagedObject从一个视图控制器传递到另一个视图控制器 - Pass NSManagedObject from one view controller to another Corebluetooth:将连接的外围设备从视图传递到另一个 - Corebluetooth: pass connected peripheral from view to another 将图像从主视图传递到另一个视图问题 - Pass image from main view to another view issue 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何通过parentviewController将数据从一个视图传递到另一个视图 - how to pass data from one view to another view through parentviewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM