Search code examples
xcode4ios7viewdidloadxcode5

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


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?


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