![](/img/trans.png)
[英]Pass data from NSViewController to another NSViewController which is in NSSplitViewController?
[英]Moving from one NSViewController to another NSViewController
我是Mac應用程序開發的新手。 自最近兩天以來,我一直面臨一個問題,但未能成功。 我的問題和在兩個不同的帶有數據的NSViewController之間夾住的問題相同。
您能否幫助我了解如何從一個NSViewController
到另一個NSViewController
的過程和語法。
我有一個用於登錄的View控制器,其中有兩個字段,即UserId
和password
。 單擊登錄按鈕后,將調用Web API來對用戶進行身份驗證,並在收到"SUCCESS"
作為響應時,應將控制權從LoginViewController
轉移到ProfileViewController
。
我已經嘗試按照問題的答案(給出的鏈接)重設此問題,但出現錯誤。 “ fromviewcontroller.view.superview不能為零。”
創建一個從LoginViewController到ProfileViewController的序列,並為其指定一個標識符,例如“ showProfile”。
@IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
performSegue(withIdentifier: "showProfile", sender: sender)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier == "showProfile" {
let profileController = segue.destinationController as! ProfileViewController
profileController.data = responseData
}
}
不使用情節提要嗎?
@IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
profileController.data = responseData
self.view.window!.contentViewController = profileController
}
}
您必須在情節提要中創建一個Segue(Ctrl +左鍵單擊LoginViewController上方的黃色圓圈按鈕並將其拖動到ProfileViewController),然后將其命名為“ showProfile”。
收到“成功”后,您要致電:
//here you enter the name of the segue you want to call
//& in sender the data you want to pass to the new VC.
performSegue(withIdentifier: "showProfile", sender: nil)
這會打電話
prepare(for segue: UIStoryboardSegue, sender: Any?)
在當前的ViewController中,因此,如果要將數據傳遞到新的ViewController,則需要重寫它。
在ViewControllers之間傳遞數據的示例:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//asking which segue is called
if segue.identifier == "showProfile" {
//when you called "showProfile", you can be sure that your
//destination is a ProfileViewController
let vc = segue.destination as! ProfileViewController
//passing the data to your new VC
vc.data = sender
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.