0

I have a view controller which get called from several places, and the first time it is called, awakeFromNib is called, but viewDidLoad is not called. viewWillAppear and viewDidAppear are called each time.

Also, the view (a UITableView subclass) functions correctly, except that anything within viewDidLoad is obviously not implemented.

loadView is not overridden in this view controller.

the code used to override viewDidLoad and awakeFromNib:

- (void)awakeFromNib
{
// Configure for self sizing cells:
 self.tableView.estimatedRowHeight = 44;
 self.tableView.rowHeight = UITableViewAutomaticDimension;

 self.tableView.allowsSelectionDuringEditing = YES;
 self.clearsSelectionOnViewWillAppear = NO;

 self.navigationItem.title = nil;
 self.navigationItem.rightBarButtonItem = nil;
 printf("\n awake from nib \n \n ---------- \n");
}

- (void)viewDidLoad
{
 [super viewDidLoad];
 self.navigationItem.rightBarButtonItem = self.editButtonItem;
 self.tableView.allowsSelectionDuringEditing = YES;
}

how is it possible that awakeFromNib is called but viewDidLoad is not, and how can I fix it?

edit: I received an answer that helped fixed it, however I would like to know why it happened, because it may be related to a bug in my app.

thank you.

Nitzan R
  • 68
  • 11
  • Please add code for these methods. And the code also where the view is initialized. – Ishan Handa Sep 24 '15 at 08:08
  • @Rage added the code. the view controller is instantiated in many places as well as the storyboard, so i don't believe it is the problem – Nitzan R Sep 24 '15 at 08:17
  • 1
    I don't think it will resolve your issue but you should call `[super awakeFromNib];` in your override. – Steve Wilford Sep 24 '15 at 08:18
  • @NitzanR If the view controller is instantiated in many places then it may exactly be the problem. It is possible viewDidLoad is called much earlier that the rest of the methods. – Ishan Handa Sep 24 '15 at 08:21
  • @SteveWilford I have no idea why, but it actually solved it! thank you! – Nitzan R Sep 24 '15 at 08:22
  • possible duplicate of [TableViewController's viewDidLoad not firing](http://stackoverflow.com/questions/23968099/tableviewcontrollers-viewdidload-not-firing) – kirander Sep 24 '15 at 08:23
  • @NitzanR does your view come from the same xib as the VC? – Daij-Djan Sep 24 '15 at 08:26
  • @Daij-Djan Yes, it does. – Nitzan R Sep 24 '15 at 08:34
  • 1
    then the view is awoken before the vc, the vc's setView is called but as it hasn't been awoken, it doesn't call viewDidLoad yet but defers it till it is awaoken. and without the super call it never knows :) q.e.d. ;) – Daij-Djan Sep 24 '15 at 08:47

1 Answers1

3

This was a bit of a stab in the dark in a comment but it turned out to resolve the issue.

Ensure to call [super awakeFromNib] in the overridden method.

Community
  • 1
  • 1
Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
  • 1
    the view is awoken before the vc, the vc's setView is called but as it hasn't been awoken, it doesn't call viewDidLoad yet but defers it till it is awaoken. and without the super call it never knows :) q.e.d. ;) -- explaining why this works – Daij-Djan Sep 24 '15 at 08:47