快速访问其他控制器的方法,动作和/或插座 [英] Accessing methods, actions and/or outlets from other controllers with swift

查看:159
本文介绍了快速访问其他控制器的方法,动作和/或插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个macOS项目中,我有一个包含两个其他ViewController的拆分视图,而且我不知道如何从主窗口的ViewController访问ViewController.

I'm working on a macOS project where I have a split view containing 2 other ViewControllers and I can't figure out how to access the ViewControllers from my primary window's ViewController.

这是设置:

基本上,我想做的是使用左上方ViewController中的Button来访问右侧SectionController中的Label,该嵌入在我的拆分视图中.

Basically what I'm trying to do is use the Button in my ViewController on the top-left to access the Label in my SectionController on the right, which is embedded in my split view.

由于无法在其他ViewController中为控件创建IBActionIBOutlet,因此我无法弄清楚如何将它们连接起来.我当前的解决方法是在我的AppDelegate上拥有一个属性,然后访问主要的共享应用程序委托,但这感觉很hacky,无法扩展.我完全不知道该如何进行.我可以使用函数将数据或其他内容传递给其他ViewController.

Since I can't create an IBAction or IBOutlet for a control in a different ViewController, I can't figure out how to get these to be connected. My current workaround has been to have a property on my AppDelegate and then access the main shared application delegate, but that feels hacky and won't scale. I'm completely lost as to how to proceed. I'm ok with using a function to pass data or whatever to the other ViewController(s).

我正在使用带有Xcode 9(测试版)的Swift 4.

I'm using Swift 4 with Xcode 9 (beta).

有什么想法吗?

推荐答案

当然,您不能在其他ViewController中为控件创建IBAction或IBOutlet! 但是,只是层次结构中的每个视图控制器都有其子视图控制器的引用.

Of course you can't create IBAction or IBOutlet for a control in a different ViewController!! But simply each view controller in the hierarchy has a reference for its child view controllers.

方法1:

@IBAction func buttonTapped(_ sender: Any) {
    let splitViewController = self.childViewControllers[0] as! YourSplitViewController
    let targetViewController = splitViewController.childViewControllers[0] as! YourTargetViewController
    targetViewController.label.text = "Whatever!"
}

方法2:

如果您在准备segue"方法中为每个子控制器提供了参考,则可能会更好

It may be better if you took a reference for each child controller in your "prepare for segue" method

ContainerViewController:

ContainerViewController:

var mySplitViewController: YourSplitViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "splitViewSegue" {
        self.mySplitViewController = segue.destination as! YourSplitViewController
    }
}

YourSplitViewController:

YourSplitViewController:

var aViewController: YourFirstViewController?
var bViewController: YourSecondViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "aViewSegue" {
        self.aViewController = segue.destination as! YourFirstViewController
    } else if segue.identifier == "bViewSegue" {
        self.bViewController = segue.destination as! YourSecondViewController
    }
}

因此,您可以像在容器视图控制器中那样访问它:

So you can access it like that in your container view controller:

@IBAction func buttonTapped(_ sender: Any) {
    self.mySplitViewController.firstViewController.label.text = "Whatever!"
}

这篇关于快速访问其他控制器的方法,动作和/或插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆