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
传递变量。我做了我能想到的一切,但没有任何效果。谁能告诉我我在做什么错?
答案 0 :(得分:0)
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)
}
}
答案 1 :(得分:0)
在另一个相同类型的实例中创建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 = ""