Xcode的两个标签之间的共享变量 [英] Shared variable between two tabs for Xcode

查看:99
本文介绍了Xcode的两个标签之间的共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器,我正在使用它们都从基本视图控制器继承


  1. 类A_ViewController:BaseViewController

  2. 类B_ViewController:BaseViewController

这两种VC都与我的firebase数据库有很大的交互作用。所以我想要一个变量来跟踪所有下载的项目,以便这两个VC可以访问它,而无需再次重新下载文件。



我试图把在BaseViewController中为两个A,B类访问的变量名称

  var allPostsDownloaded:[Post]!因此,在A_VC下载任何数据之前,它会检查此allPostsDownloaded变量,并在数据存在的情况下从中加载该变量。如果它不存在,我追加到这个变量。所以当切换到B_VC时,可以做同样的事情,不需要时不重新下载数据。 



原因我没有使用segue或protocal来传递数据,这是因为两个VC与我的数据库相互影响很大。因此,尝试使用一个可变的数据来记录事物的位置是很清晰的。



但是,问题是我

  var allPostsDownloaded:[邮政]! 

每次在A和B之间切换时都会被调用(这是标签)。这将导致变量为空并且被解除初始化。

我想我可以使用全局变量,但那不是好习惯吗?任何人都可以解释为什么新标签出现时会重新调用它?这是最好的解决方案。

解决方案

由于@avi提到了创建新的单例类,那么您可以轻松地传递和阅读。下面是一个例子

  class PersistentData 
{
static let sharedInstance = PersistentData()

//您的全局持久变量
var allPostsDownloaded = [Post]()
}

因此,在你的控制器中,你可以简单地读取和设置如下

  // read 
print (PersistentData.sharedInstance.allPostsDownloaded)

//设置新数据。这个例子,因此取决于你的情况
PersistentData.sharedInstance.allPostsDownloaded.append(newPost)
PersistentData.sharedInstance.allPostsDownloaded = posts

也要记住,如果你想在标签之间切换时读取新值,你可以在 viewDidAppear viewWillAppear


I have two view controllers that I am working on which both inherits from a Base view controller

  1. class A_ViewController: BaseViewController
  2. class B_ViewController: BaseViewController

Both of those VC interacts heavily with my firebase database. So I want a variable to keep track of all the downloaded items so those two VC can access it without the need to re-download the file again.

I tried to put a variable name in BaseViewController for the two A,B class to access

    var allPostsDownloaded:  [Post]!

So before A_VC downloads any data, it checks for this allPostsDownloaded variable and loads from it if the data exists. If it doesnt exist, I append to this variable. So when switching to B_VC, the same can be done and no data is re-downloaded when not required.

Reason I am not using segue or protocal to pass data around is that the two VC interacts quite heavly with my database. So it was alot cleaner to try and have a mutural data varaible to keep track of where things are.

However, the problem is that i

var allPostsDownloaded:  [Post]!

gets called whenever I switch between A and B VC (Which are tabs). This will cause the variable to be empty and de-initialised.

I guess I could use a global variable instead but that is not good practice? Could anyone please explain why it gets re-called when new tab shows up? And the best solution for this.

解决方案

as @avi mentioned create new singleton class, then you can pass and read easily. Below is an example

class PersistentData
{
    static let sharedInstance = PersistentData()

    // your global persistent variable
    var allPostsDownloaded = [Post]()
}

So in your controllers you can simple read and set as below

// read
print(PersistentData.sharedInstance.allPostsDownloaded)

// set new data. this just example, hence depends on your case
PersistentData.sharedInstance.allPostsDownloaded.append(newPost)
PersistentData.sharedInstance.allPostsDownloaded = posts

also keep in mind that if you want to read new value when switching between tabs, you can get the updated in viewDidAppear or viewWillAppear

这篇关于Xcode的两个标签之间的共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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