将数据从ViewController传递到TabViewController [英] Passing data from a ViewController to a TabViewController

查看:108
本文介绍了将数据从ViewController传递到TabViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最有效的方法是将变量(例如:字符串)从一个常规ViewController传递到TabViewController. (不是iOS =>仅OSX) 我一直在搜索并尝试不同的东西,我知道这里有"prepareForSegue"功能,但是我似乎无法使其在Tab View中正常工作. (我正在尝试将50-70个以上的变量传递给下一个视图,顺便说一句)是否无法创建一个单独的文件,我可以在其中存储所有变量,然后将其导入新的视图控制器中?我知道这在目标c中有效. 我尝试了一种类似的方法在Swift中进行操作,但是当我在第二个视图控制器中调用它们时,这些值似乎并没有存储在vars中.

I would like to know what the most efficient way is to pass a variable (say: a string) from one regular ViewController to a TabViewController. (NOT iOS => ONLY OSX) I've been searching and trying different stuff and I know there is the "prepareForSegue" function, but I can't seem te get it working for a Tab View. (I'm trying to pass over 50-70 vars to the next view btw) Is there no way to create a separate file in which I can store all my vars and then import it in the new view controller? I know this works in objective c. I tried a similar way to do it in Swift but the values just don't seem te remain stored in the vars when I call them in the second view controller.

请不要开始链接到常规视图控制器的教程,因为我已经阅读了其中的10个,而且似乎都没有帮助.

Pls don't start linking to tutorials for regular view controllers because I've already read 10 of them and none of them seem to help.

我还下载了Swift手册,并检查了Xcode中的文档,对我来说还不清楚.

I've also downloaded the Swift manual and checked the documentation in Xcode and it's just unclear to me.

PS:X代码是纯净的,完全是sh * t.我认为从苹果那里得到的东西会比这个无用的IDE更容易获得.只是想通风...

PS: X-code is pure and utter sh*t. I thought that something from apple would be more accessible then this useless piece of IDE. Just wanted to ventilate that...

推荐答案

虽然您找到了答案,但这是使用Swift的方法

Althought you find your answer, here is way to do it with Swift

import Cocoa

@objc protocol SomeDelegate {
    func passData(sender: ViewController, data: String)
}

class ViewController: NSViewController {

    weak var delegate: SomeDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let myTabViewController = parentViewController as? MyTabViewController {
            self.delegate = myTabViewController
        }
    }

    @IBAction func buttonPressed(sender: AnyObject) {
        delegate?.passData(self, data: "Hello, world!")
    }
}

在标签视图控制器中:

class MyTabViewController: NSTabViewController, SomeDelegate {

    func passData(sender: ViewController, data: String) {
        print("Data is \(data)")
    }
}

这篇关于将数据从ViewController传递到TabViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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