简体   繁体   English

如何在iOS中管理ViewController

[英]how to manage viewControllers in iOS

I have a problem with transfering data between viewcontrollers. 我在ViewController之间传输数据时遇到问题。 I`m now working on a small app, it has a main viewcontroller,and in this viewcontroller user can change some global data that all other viewcontrollers will use to fetch data from the internet. 我现在正在开发一个小型应用程序,它有一个主ViewController,在该ViewController中,用户可以更改一些全局数据,所有其他ViewController都将使用这些全局数据从Internet上获取数据。 So, when the global data changes I need to tell other viewcontrollers to update data. 因此,当全局数据更改时,我需要告诉其他视图控制器更新数据。

The question is how can I tell other viewcontrollers when the global data changes ? 问题是, 当全局数据更改时,如何告诉其他视图控制器

I know I should use NSNotificationCenter to solve this problem, but I feel notification is like a foreigner to all the viewcontrollers. 我知道我应该使用NSNotificationCenter解决此问题,但是我觉得通知对于所有的视图控制器来说都是外国人。

What I want to do is to add a ViewControllerManager to manage all the viewControllers. 我想要做的是添加一个ViewControllerManager来管理所有viewControllers。 Firstly, I would add all the viewcontroller who need to use the global data to the manager. 首先,我将需要使用全局数据的所有视图控制器添加到管理器中。 Then ,when the global data changes I could find these viewcontrollers through the manager,and notify them to update the global data directly. 然后,当全局数据更改时,我可以通过管理器找到这些视图控制器,并通知它们直接更新全局数据。

and this is my @interface. 这是我的@interface。

@interface ViewControllerManager : NSObject
+ (ViewControllerManager *)sharedManager;
- (void)addViewController: (UIViewController *)viewController;
- (void)removeViewController: (UIViewController *)viewController;
- (void)removeViewControllerByClass: (Class)aClass;

- (UIViewController *)viewControllerByClass: (Class)aClass;
- (NSArray *)viewControllersByClass: (Class)aClass;

//get viewcontrollers who can response to selector, so I can send them this message.
- (NSArray *)viewControllersResponseTo: (SEL) selector;

//get current visible viewcontroller.
@property (nonatomic, retain) UIViewController * visibleViewController;

@end

It works well, through this manager I can get any viewcontroller I want, and I can transfer global data easily between viewcontrollers, But the problem is I don't know how to manage the memory correctly , in method [ViewControllerManager addViewController: ] , I retain the viewController ,But I don`t know when to release it ,so this viewController will never be dealloc... 它运行良好,可以通过该管理器获得所需的任何ViewController,并且可以在ViewController之间轻松地传输全局数据,但是问题是我不知道如何正确地管理方法[ViewControllerManager addViewController: ] 的内存 ,我保留viewController,但是我不知道什么时候发布它,所以这个viewController将永远不会被释放...

The latest point in time where you have a possibility to release is in your manager class's dealloc method. 您有可能发布的最新时间是在经理类的dealloc方法中。 If you store those controllers in an array at the moment of adding them the array also retain them. 如果在添加这些控制器时将它们存储在阵列中,则该阵列也将保留它们。 It might be a solution to retain and release that array only, not the controllers. 这可能是仅保留和释放该阵列而不是控制器的解决方案。 When an item is removed from an array the array does the necessary releasing. 从数组中删除项目时,数组会进行必要的释放。 Also when an array is gone (released), the items are gone as well (automatically). 同样,当数组消失(释放)时,项目也消失(自动)。

By the way what you are doing is called Mediator pattern I think. 顺便说一句,我认为您正在做的事情称为中介者模式。 The controllers talk only to a central object not to each other. 控制器仅与中央对象通话,而不与对方通话。 Only the mediator knows all the actors. 只有调解员才知道所有演员。

最后,我通过使用弱引用解决了这个问题,这是代码: https : //github.com/github-xiaogang/ViewControllerManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM