Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weak or strong properties for my sub-viewControllers?

#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?

like image 585
MJB Avatar asked May 07 '12 19:05

MJB


2 Answers

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.

like image 50
Johannes Lumpe Avatar answered Oct 11 '22 14:10

Johannes Lumpe


Strong is the equivalent of retain except that ARC will manage the releasing for you. Weak is for avoiding retain cycles - where the parent retains the child and vice versa.

__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.

Here's a link: Weak and strong property setter attributes in Objective-C

Given that you want to keep these view controllers around, the best option is a strong reference! Make sure they aren't released before you need them!

like image 41
waylonion Avatar answered Oct 11 '22 13:10

waylonion