From a249588f52b9eb7f2130fc9ff474594db88b72f9 Mon Sep 17 00:00:00 2001 From: Aparajita Fishman Date: Thu, 22 Mar 2012 14:22:20 -0700 Subject: [PATCH 1/2] UIKit does NOT call viewDidLoad from setView, made this explicit in the docs viewDidLoad is meant to deal with asynchronous *lazy* instantiation of a view via a cib or loadView. setView is neither asynchronous nor lazy. --- AppKit/CPViewController.j | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/AppKit/CPViewController.j b/AppKit/CPViewController.j index f5a3230d3..1a56c7056 100644 --- a/AppKit/CPViewController.j +++ b/AppKit/CPViewController.j @@ -139,8 +139,8 @@ var CPViewControllerCachedCibs; implementation for programmatic views is to create a plain view. You can invoke super to utilize this view. - If you use Interface Builder to create your views, you initialize the view - using the initWithCibName:bundle: method then you must not override this + If you use Interface Builder to create your views, and you initialize the view + using the initWithCibName:bundle: method, then you MUST NOT override this method. The consequences risk shattering the space-time continuum. Note: The cib loading system is currently synchronous. @@ -208,9 +208,9 @@ var CPViewControllerCachedCibs; This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib - file or created programmatically in the loadView method. This method is - most commonly used to perform additional initialization steps on views - that are loaded from cib files. + file or created programmatically in the loadView method, but NOT when setView + is invoked. This method is most commonly used to perform additional initialization + steps on views that are loaded from cib files. */ - (void)viewDidLoad { @@ -228,13 +228,7 @@ var CPViewControllerCachedCibs; */ - (void)setView:(CPView)aView { - var viewWasLoaded = !_view; - _view = aView; - - // Make sure the viewDidLoad method is called if the view is set directly - if (!_isLoading && viewWasLoaded) - [self viewDidLoad]; } @end From 88783efa7b32dde2c1423432735b0bef37beed71 Mon Sep 17 00:00:00 2001 From: Aparajita Fishman Date: Thu, 22 Mar 2012 15:43:18 -0700 Subject: [PATCH 2/2] Needed an _isLazy flag to get correct behavior --- AppKit/CPViewController.j | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/AppKit/CPViewController.j b/AppKit/CPViewController.j index 1a56c7056..64a32d4f9 100644 --- a/AppKit/CPViewController.j +++ b/AppKit/CPViewController.j @@ -66,6 +66,7 @@ var CPViewControllerCachedCibs; { CPView _view @accessors(property=view); BOOL _isLoading; + BOOL _isLazy; id _representedObject @accessors(property=representedObject); CPString _title @accessors(property=title); @@ -124,6 +125,7 @@ var CPViewControllerCachedCibs; _cibExternalNameTable = anExternalNameTable || [CPDictionary dictionaryWithObject:self forKey:CPCibOwner]; _isLoading = NO; + _isLazy = NO; } return self; @@ -199,6 +201,11 @@ var CPViewControllerCachedCibs; _isLoading = NO; [self viewDidLoad]; } + else if (_isLazy) + { + _isLazy = NO; + [self viewDidLoad]; + } return _view; } @@ -260,6 +267,7 @@ var CPViewControllerViewKey = @"CPViewControllerViewKey", _cibBundle = bundlePath ? [CPBundle bundleWithPath:bundlePath] : [CPBundle mainBundle]; _cibExternalNameTable = [CPDictionary dictionaryWithObject:self forKey:CPCibOwner]; + _isLazy = YES; } return self;