#import <UIKit/UIKit.h>
#import "UCMapviewController.h"
#import "UCMenuviewController.h"
#import "UCOverviewController.h"
@interface UCRootViewController : UIViewController
@property (weak, nonatomic) UCMapviewController *mapviewController;
@property (weak, nonatomic) UCMenuviewController *menuviewController;
@property (weak, nonatomic) UCOverviewController *overviewController;
This is the declaration of my UCRootViewController which should manage these sub-viewControllers. He will later also become their delegate to handle when one controller should be shown or not.
The rootViewController is held strong in the UIAppDelegate and will remain active all the time.
So is it correct to make those sub-viewControllers weak? I’m not 100% sure, but as far as I understand weak pointers get deallocated when there is no strong pointer pointing to them. So as the root is strong, it’s correct to make them weak, right?
#import <UIKit/UIKit.h>
@class UCRootViewController;
@interface UCOverviewController : UIViewController
@property (weak, nonatomic) UCRootViewController *rootviewController;
This is the header of one of my sub-viewControllers. They have a weak pointer to the (later) delegate rootviewController. Is it enough to declare @class UCRootviewController to make them call the delegate methods? Do I even need this?
thanks
EDIT: I just read a nice article about ViewControllers and the passage:
Always use high-quality view controller containers or
+[UIViewController presentModalViewController:animated:] to display view controllers in your application. If you need to keep a reference
to a view controller somewhere, use a weak reference, except if you
really want it to stay alive longer for caching purposes. In such
cases, be sure to correctly respond to low-memory conditions.
It says use a weak reference, what’s your opinions on this?
The strong pointer only ties into your rootViewController. This doesn’t mean that it will automatically strong point to your other viewControllers inside your rootViewController.
You should set those properties to strong, to make sure that your other viewControllers won’t get deallocated in any way you don’t want them to.
If you push them into a navigation stack it will be fine, because the navigation stack automatically strongs points to them. But if you’re just going to add their views as subviews in your rootViewController, then you want to make sure that those properties are strong.
Objects get deallocated when there is no strong pointer to them. And as there are no strong pointers to your other viewControllers they will get deallocated.