iOS7 / XCode5 - "viewDidLoad" called at once, rather than when "presentViewController" was called

StackOverflow https://stackoverflow.com/questions/18955249

Question

I have just upgraded to XCode5 and iOS7 and now my application has stopped working.

I am creating a new view based on a property of a current view, and I need to set some properties of the new view before I display it.

Previously, I did it like this :-

hqView *v = [[hqView alloc] initWithNibName:NULL bundle:NULL];
[v setProperty1:true];
[v setProperty2:false];

[self presentViewController:v animated:TRUE completion:NULL];

This then triggered the [viewDidLoad] method on the view controller, which had the following code in it :-

if ([self property1])
{
 [list1 load]
}
else
{
 [list2 load]
}

However now the [viewDidLoad] method is triggering as soon as I create the view, meaning that I am not able to set the properties before [viewDidLoad] is called and so it ALWAYS loads list2 regardless of what I actually want.

The thing is - this did NOT happen under iOS6, so I am wondering whether it is a new setting in XCode5 that has caused this to change, or if I am going to have to rewrite it to do what I need it to do?

Was it helpful?

Solution

You cannot know when viewDidLoad, viewWillAppear, etc... will be called.

My advice : Make a dedicated init method to your controller, something like :

@implementation hqView

- (instancetype)initWithProperty1:(BOOL)prop1 property2:(BOOL)prop2
{
   // uses default NIB
   self = [super initWithNibName:nil bundle:nil];
   if (self){
      [self setProperty1:prop1];
      [self setProperty2:prop2];  

   }
   return self;
}


@end

OTHER TIPS

Set a breakpoint on your viewDidLoad method that is being called before your init method and you will be able to see what is causing the viewDidLoad to be called. you will probably find that it is being called because the view was referenced by some other code. this most often happens in a super class (like if you have a UIViewController superclass that implements common functionality for your view controllers). for example, if you accidentally put new code in that accessed self.view in a method in your superclass such as - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil you would notice this behavior. so make sure you are not accessing the view in any code before you wanted to. -rrh

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top