3

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?

Angelholme
  • 35
  • 1
  • 6

2 Answers2

2

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
Vinzzz
  • 11,746
  • 5
  • 36
  • 42
  • I was hoping to avoid this, because there are some points where I have to set five or six variables. But doing that is better than doing it in viewWillAppear at least :) – Angelholme Sep 23 '13 at 10:33
  • What I don't get is why it has suddenly changed? Or was it just dumb luck it worked EVERY TIME under iOS6/XCode4 and doesn't work at all under iOS7/XCode5? – Angelholme Sep 23 '13 at 10:34
  • Thank you anyway - I would vote you up for the answer but I apparently don't have enough points to do that yet - sorry! – Angelholme Sep 23 '13 at 10:35
  • Indeed, it's intriguing that `viewDidLoad` gets called, even when `presentViewController` is commented... Do you define any parent/child relationship between your `viewcontrollers`yourself ? – Vinzzz Sep 23 '13 at 10:37
  • Not really - I just create the child view, set the properties, call it and then go from there. No storyboards, no all-powerful controller - just create them as I go. – Angelholme Sep 23 '13 at 10:51
1

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

Richie Hyatt
  • 2,244
  • 2
  • 21
  • 14