将数据从NSViewController传递到NSSplitViewController中的另一个NSViewController?

时间:2018-07-03 14:56:53

标签: swift macos nsviewcontroller

您好,我无法将数据发送到NSSplitViewController。似乎在IOS开发中无法正常工作。

firstController:

class LoginController: NSViewController {
 //.....
  @IBAction func Login(_ sender: Any) {
     //....
     let story = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), 
                bundle: nil).instantiateController(withIdentifier: 
                NSStoryboard.SceneIdentifier(rawValue: "SPLIT")) as! 
                NSSplitViewController
      let vc : ContainerController = ContainerController();
      vc.passedData = "karosas";
      self.view.window?.contentViewController = story;
     //....
   }
 //.....
}

第二个NSSplitViewController:

import Cocoa

class ContainerController: NSSplitViewController {

    var passedData = "";

    override func viewDidLoad() {
        super.viewDidLoad()
        print(passedData);
        print("nzn");
    }

}

现在我正在尝试将数据发送到NSViewSplitController。我需要将数据传递到NSSplitViewController中的NSViewController。我确实尝试了许多不同的方法,但是我失败了。所以我认为我需要先将数据传递给SplitController,然后再传递给ViewController。但是我什至不能发送任何数据到NSSplitViewController。有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

我已经使用这段代码做到了这一点。

        self.userName = value["name"] as! String;
        let story = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
        .instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SPLIT")) 
        as! ContainerController
        let dest = story.childViewControllers[0] as! MenuController;
        dest.passData = self.userName;
        self.view.window?.contentViewController = story;

我不知道这是一个好方法,但是它行得通。

答案 1 :(得分:0)

如果LoginController和MenuController都在ContainerController中,则可以执行以下操作:

LoginController

import Cocoa

class LoginController: NSViewController {
 //.....
  @IBAction func Login(_ sender: Any) {
     //....
     self.userName = value["name"] as! String
     let MenuVC = parent?.childViewControllers[1] as! MenuController // 0 or 1 depending on order in SplitView
      MenuVC.passedData = self.username
      self.view.window?.contentViewController = parent as! ContainerController
     //....
   }
 //.....
}

MenuController

import Cocoa

class MenuController: NSViewController {

    var passedData = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        print(passedData)
        print("nzn")
    }

}