Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController -viewDidLoad not being called

Being new to Cocoa, I'm having a few issues with Interface Builder, UIViewController and friends.

I have a UIViewController subclass with a UIView defined in a xib, and with the controller's view outlet connected to the view. The xib's "file's owner" is set as myViewcontroller subclass.

In this one instance, the following code to load the controller/view (from the main view controller) doesn't work as expected:

if ( self.myViewController == nil ) {     self.myViewController = [[MyViewController alloc]         initWithNibName:@"MyViewController" bundle:nil]; }  [self.navigationController      pushViewController:self.myViewController animated:YES]; 

In MyViewController's methods, I have placed breakpoints and log messages to see what is going on:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {      if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {         NSLog(@"initWithNibName\n");     }      return self; }  -(void)viewDidLoad {      [super viewDidLoad];     NSLog(@"viewDidLoad\n"); } 

Expected result

Both -initWithNibName and -viewDidLoad methods are called, and myViewController's view is displayed.

Observed result

Only -initWithNibName is called, the view is not displayed.

Have I missed something? Can anyone recommend anything to check? (Particularly in the wondrously opaque Interface Builder tool).

like image 969
Justicle Avatar asked May 27 '09 02:05

Justicle


People also ask

Does viewWillAppear get called before viewDidLoad?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

How many times is viewWillAppear called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears.

What is the role of UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

RE: SOLUTION FOUND!!!!!

Indeed that seems to be a working solution, however the real trick is not in setting the view.hidden property to NO, what makes the view load from the nib file is the calling of the UIViewController's view method, the view only actually gets loaded from the nib when the view method is called for the first time.

In that sense, a simple [viewController view] message would force the view to load from the nib file.

like image 149
Jader Feijo Avatar answered Oct 09 '22 21:10

Jader Feijo