Why is webview readonly?


In the Web View Controller, webView’s property is set to readonly. Then the get method returns a webview that is allocated and initialised in the loadView method and then set to the view.

  • (void)loadView {
    // Create an instance of UIWebView as large as the screen
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];

    // Tell web view to scale web content tot fit within bounds of webview

    [wv setScalesPageToFit:YES];

    [self setView:wv];

#pragma mark -

  • (UIWebView *)webView {
    return (UIWebView *)[self view];

Why isn’t webView just declared as readwrite and allocated and initialised in the loadView like an ivar? Then the getter could just return _webView.


This reply is a little late, but I assume it’s because the webview is to serve as the view for the WebViewController. Letting it have it’s own read/write instance variable would allow for the possibility of having a view assigned to the view property of the view controller other than our web view, and this would be bad. I think we could have done away w/ the instance variable altogether and just prefaced any regular call to [WVCinstance view] with (UIWebview *) in order to let the computer know that a UIWebview is serving as the VC’s view, but this would become tedious so the book’s approach seems best. It’s kind of like we’re overriding the default view “getter”, but have to do a little extra work in order to get the correct return type of UIWebview *.