如何轻松地将变量从一个视图传递到另一个视图? [英] How can I pass a variable from one view to another easily?

查看:25
本文介绍了如何轻松地将变量从一个视图传递到另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望能够轻松地将变量从一个视图传递到另一个视图.我已经通过 Segue 看到了一些方法,但如果可能的话,我希望有比这更简单的方法.

So I want to be able to pass a variable from one view to another view easily. I've seen ways by doing it via a Segue but I was hoping for something simpler than that if possible.

有什么方法可以轻松地传输一个变量的内容并将其保存在另一个视图中.我知道这可能是不可能的.

Is there any way to just transfer the content of one variable and save it in another view really easily. I am aware this might not be possible.

如果这是不可能的,请有人解释如何使用 Segue 方法?我看过各种在线教程,解释了如何做到这一点,但他们都将其应用于某种情况.我只想知道裸代码及其工作原理,然后将其应用于我自己的情况.所以基本上只是一种将变量从一个视图转移到另一个视图的方法,像我这样知识很少的人会理解.我理解术语,因为我用其他语言编程,但 swift 对我来说很陌生.

If this isn't possible, please could someone explain how to use the Segue method? I've seen various online tutorials explaining how to do it but they are all applying it to a situation. I just want to know the bare code and how it works then apply it to my own situation. So just basically a way to transfer a variable from one view to another that someone with very very little knowledge like me will understand. I understand terminology as I program in other languages but swift is very new to me.

我应该补充一点,我使用的编码语言是快速的.

I should add that the coding language I am using is swift.

如果有人能帮我解决这个问题,我将不胜感激.

I would appreciate it if someone could help me with this.

谢谢

推荐答案

如果你发送的只是一个变量,我会使用prepare for segue.

If it just one variable that you are sending, i would use prepare for segue.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "IdentifierOfSegueGoesHere"
    {
        if let destinationVC = segue.destinationViewController as? OtherViewController{
            destinationVC.dictionary = self.dictionary
        }
    }
}

如果您要传递大量数据,例如一个朋友列表等然后我会创建一个单例类来存储它们,可以从任何地方访问.

If you are passing a lot of data, e.g. a list of friends etc then i would create a singleton class that stores them that can be accessed from anywhere.

如果您传递的多个变量没有足够的连接来创建单例类,那么您可以将它们放入字典并传递字典.

If you are passing multiple variables that don't have enough of a connection to create a singleton class then you can put them into a dictionary and pass the dictionary.

这篇关于如何轻松地将变量从一个视图传递到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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