Swift4 Xcode9 通过 segue 在 ViewController 之间传递变量

Swift4 Xcode9 Passing variable between ViewControllers through segue

class InstanceViewController: NSViewController{
    @IBOutlet weak var InstanceAddr:NSTextField!
    var input: String = ""
    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        InstanceViewController().input = "https://google.com"
        if (segue.identifier?.rawValue == "moveToLoginWindow"){
            let destinationVC = segue.destinationController as! loginWebViewController
            destinationVC.address = input
        }
    }
}

class loginWebViewController: NSViewController{
    @IBOutlet weak var instanceview: WKWebView!
    var address: String?
    override func viewDidLoad(){
        var instanceaddress: String? = address
        super.viewDidLoad()
        var url = URL(string: instanceaddress!)
        var request = URLRequest(url: url!)
        instanceview.load(request)
    }
}

我正在尝试将变量 input: String 从 InstanceViewController 传递到 loginWebViewController 中的 address: String? 并使用此地址加载 webview。我成功加载了 webview,但我似乎无法通过 segue moveToLoginWindow 传递变量。我做了我能想到的一切,但没有任何效果。谁能告诉我我做错了什么?

class InstanceViewController: NSViewController{
    @IBOutlet weak var InstanceAddr:NSTextField!
    var input: String = ""
    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        InstanceViewController().input = "https://google.com" // Well actually in this line you are creating a new instance for your controller , that will not affect the input value of this controller So correction will be :-
        self.input = "https://google.com"
        if (segue.identifier?.rawValue == "moveToLoginWindow"){
            let destinationVC = segue.destinationController as! loginWebViewController
            destinationVC.address = input
        }
    }
}

class loginWebViewController: NSViewController{
    @IBOutlet weak var instanceview: WKWebView!
    var address: String?
    override func viewDidLoad(){
        var instanceaddress: String? = address // there is no need of create extra object as both are optional Prefer  guard let instanceaddress = address else { return }
        super.viewDidLoad()
        var url = URL(string: instanceaddress!)
        var request = URLRequest(url: url!)
        instanceview.load(request)
    }
}

这可能对您有帮助:-

class InstanceViewController: NSViewController{
    @IBOutlet weak var InstanceAddr:NSTextField!
    var input: String = ""
    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        self.input = "https://google.com"
        if (segue.identifier?.rawValue == "moveToLoginWindow"){
            let destinationVC = segue.destinationController as! loginWebViewController
            destinationVC.address = input
        }
    }
}

class loginWebViewController: NSViewController{
    @IBOutlet weak var instanceview: WKWebView!
    var address: String?
    override func viewDidLoad(){
        super.viewDidLoad()
        guard let instanceAddress = address, let url = URL(string: instanceAddress) else { return}
        var request = URLRequest(url: url)
        instanceview.load(request)
    }
}

在另一个相同类型的实例中创建 InstanceViewController 的新实例毫无意义并会导致问题。

您必须使用当前实例的input

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    input = "https://google.com"
    if segue.identifier?.rawValue == "moveToLoginWindow" {
        let destinationVC = segue.destinationController as! loginWebViewController
        destinationVC.address = input
    }
}

并且由于传递的类型是非可选的,所以声明 address 也是非可选的

var address = ""