In fact, you can’t simply write
because the property webView is not synthesized in WebViewController.m, therefore neither a webView instance variable nor a webView getter method are generated to support this readonly property. The reasoning behind the peculiar treatment of the webView property is interesting – if I grasped it correctly when studying chapter 25, of course.
This is the explanation I ended up with. WebViewController already has a reference to the UIWebView in its view instance variable – and WebViewController’s explicit webView getter method does use that to return the view (with a convenient cast from UIView to UIWebView). A webView instance variable would then be a cumbersome double of WebViewController’s inherited view property.
Most importantly, a second (strong) reference to the view would make it impossible for the system to sweep it from memory in case of a low memory warning (checked with Instruments). The view variable is set to nil when a low memory warning occurs, but our webView variable is not, so the web view remains alive – at least until the next time we select a row in the master table view and a new web view is allocated.
An alternative to the approach taken in the text could be making webView a weak property, synthesizing it in WebViewController.m (no need to implement an explicit getter method for it in this case) and assigning to it with care:
[code]@interface WebViewController : UIViewController
@property (nonatomic, weak, readonly) UIWebView *webView;
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];
webView = wv;
In loadView we can’t write
because webView, at that moment, is the only reference to the newly created object, but it is weak, so the object is immediately destroyed. We must put up a strong reference (wv) before assigning to webView, in order to maintain the UIWebView alive for the duration of the method. At the end of the method, the WebViewController takes ownership of the UIWebView with its view instance variable.