0
votes

My view heirarchy sits on several custom "root" UIViewController subclasses. I'm trying to set a custom self.view for one of my root VC subclasses. There, I am doing:

MyRootViewController_With_Scroll.h

// Import lowest level root VC
#import "MyRootViewController.h"
// my custom scroll view I want to use as self.view
@class MyScrollView;

@interface MyRootViewController_With_Scroll : MyRootViewController {

}

@property (strong) MyScrollView *view;

@end

MyRootViewController_With_Scroll.m

#import MyRootViewController_With_Scroll.h;

@interface MyRootViewController_With_Scroll ()

@end

@implementation MyRootViewController_With_Scroll

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)loadView
{
    NSLog(@"loading view");
    CGRect windowSize = [UIScreen mainScreen].applicationFrame;
    MyScrollView *rootScrollView = [MyScrollView scrollerWithSize:windowSize.size];
    self.view = rootScrollView;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Getter and setter for self.view
- (MyScrollView *)view
{
    return (MyScrollView *)[super view];
}
- (void)setView:(MyScrollView *)view
{
    [super setView:view];
}

According to the iOS6 docs, viewDidLoad in only suppose to fire once per view controller for the entire lifecycle of the app.

What am I doing wrong here? What is causing my view controllers to repeatedly call loadView/viewDidLoad? Strangely, my apps "home screen" VC loads the view just once, but all its subsequent views in the navigation heirachy fires loadView everytime they appear. What is going on?

edit I've changed the property to strong. Same issue happens.

edit 2 I've stopped overriding loadView and its still happening. Now I'm really confused.

2
@dasblinkenlight changed the property to strong and same issue occurs.zakdances

2 Answers

2
votes

This is expected behaviour. If you're popping view controllers off a navigation stack, and nothing else has a reference to them, then they're going to get deallocated. Therefore when it appears again, it will be a new instance, so it has to perform loadView and so on all over again. Include self in your logging, you should see that it is a different object each time.

You've also redefined the view controller's view property as weak - if you are re-using the view controller objects, then this will be nilled out as soon as the view has no superview.

Prior to iOS 6, a view controller that was mid-way in your navigation stack would get its view unloaded under memory pressure:

root --> VC1 --> VC2

VC2 is on screen, a memory warning is received. VC1 would unload its view. When you pop VC2 off the stack, VC1 would call loadView again. This no longer happens.

However, if you've popped back to VC1, and nothing has a strong reference to VC2, then VC2 is deallocated. When you push another VC2 onto the stack, this is a new instance and loadView will be called again. Presumably you are creating these new instances of VC2 in code, so you should be able to tell that you are creating a new instance.

0
votes

Thats because you have weak view property. So it get realloced all the time. Also, I don't think that you need to override view property at all.