如何使数据对所有视图控制器可见? [英] How to make data visible for all view controllers?

查看:60
本文介绍了如何使数据对所有视图控制器可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下情况:

我有一个标签栏应用程序,点击每个标签栏项目会将用户带到由不同视图控制器处理的其他视图(典型模式).

I have a tab bar application where tapping each tab bar item takes user to other view that is handled by different view controller(typical pattern).

在我的一个控制器中,我有下载重要数据的方法,我希望它们对整个应用程序都是全局的.我应该使用什么设计模式?

In one of my controllers I have method that downloads the important data and I want them to be global for whole application. What design pattern should I use?

一种方法是使用诸如核心数据之类的持久性来存储这些数据,但这是使数据对所有视图控制器可见的唯一方法吗?也许应用委托能够执行此类操作?

One way to do that is to store this data using persistence such as core data, but is it the only way to make data visible for all view controllers? Maybe app delegate is able to perform such actions?

一般来说,如果您有一些数据或变量应该对项目中的所有视图控制器可见,您通常如何解决这种情况?

How in general you solve such situation where you have some data or variable which should be visible for all view controllers in your project?

请注意,我不是在问在应用程序启动时保留数据的问题,我只是想知道如何在整个项目中使某些数据具有全局性.

Note that I'm not asking about persisting data across launches of the app, I just wonder how to make some data global in terms of the whole project.

推荐答案

不要(强调不要)使用以下内容:

  • 单身人士
  • AppDelegate(只是另一个单例)
  • NSUserDefaults
    • 核心数据
    • 在实例化期间或通过属性传入

    不要搞乱你的记忆
    相反,不要与 SOLID 的几个原则混为一谈.

    The DON'Ts messes up your memory
    the Rather Don't messes with several principals of SOLID.

    • 创建一个基础视图控制器,该控制器具有接收您的数据的属性,并使您的所有视图控制器都继承自它.

    • Create a base view controller that has a property that takes your data, make all your view controller inherit from it.

    子类 UITabBarController

    subclass UITabBarController

    如果选择了新的视图控制器,将数据设置到视图控制器

    if a new view controller is selected, set the data to the view controller

    实现有点棘手,这是来自真实世界的应用程序

    the implementation is a bit tricky, this is from a real world app

    class ContentTabBarController : UITabBarController {
        
        private var kvoSelectedViewControllerContext: UInt8 = 1
        
        required init(coder aDecoder: NSCoder) {
            
       
            self.addObserver(self, forKeyPath: "selectedViewController", options: .New | .Old | .Initial , context: &kvoSelectedViewControllerContext)
        }
        
        deinit{
            self.removeObserver(self, forKeyPath: "selectedViewController")
        }
        
        override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
            if context == &kvoSelectedViewControllerContext {
                var targetVC : UIViewController?
    
                if let viewController = change["new"] as? UIViewController{
                    if let oldViewController = change["old"] as? UIViewController{
                        if viewController != oldViewController {
                            targetVC = viewController
                        }
                    }
                } else {
                    targetVC = self.viewControllers![0] as? UIViewController
                }
                self.configureTargetViewController(targetVC)
            }
        }
        
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            self.navigationController?.navigationBar.translucent = false
        }
    
        func configureTargetViewController(viewController: UIViewController?){
    
            //setup data
        }
    }
    

    标签栏控制器如何获取数据.

    好吧,这取决于你.它可以获取核心数据,它实际上可以将获取管理器作为数据传递.它可以从光盘或网络读取.但是对于一个理智的设计,它不应该自己做,而应该使用另一个类.并且这个 fetcher 类的一个实例应该从标签栏控制器的外部设置,即从 App Delegate.

    How does the tab bar controller get the data.

    Well, that is up to you. It could fetch core data, it could actually pass a fetching manager as data. It could read from disc or network. But for a sane design it should not do it itself but use another class for it. and an instance of this fetcher class should be set from outside the tab bar controller, i.e. from the App Delegate.

    这篇关于如何使数据对所有视图控制器可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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