Silver Challenge Frustrations


#1

The silver challenge was pretty frustrating. I first tried to instantiate a UIToolbar instance with code and place it on the BNRWebViewController which as a UIViewController that had its main view reassigned to a UIWebView’s view. For functionality I wanted to place the UIToolBar near the top but under the Navigation Controller toolbar. I was successful in doing this by setting the UIToolBar frame, but it would overlap the main view (i.e. the top of the web view). I tried multiple ways to adjust the frame of the web view so I could shift its top edge down to make room for the UIToolBar, but I ran into problems due to the view hierarchy. So then I tired leaving a default view of the UIViewController in place then adding the UIWebView as a subview. Even so, I was never successful in making it look nice. Finally I gave up following the instructions explicitly (create a UIToolbar instance) since it was a better aesthetic and functional result by simply setting an array of two UIBarButtonItem instances (Back, Forward) on the rightBarButtomItems property of the UINavigationItem. This displayed what I wanted. Once I had the UI programmatically set up, then it was fairly easy to set self as a delegate of the UIWebView. Then I caught the webViewDidFinishLoad delegate method to ask the web view if “canGoBack” and “canGoForward” so I could enable/disable the buttons accordingly.

Here is the BNRWebViewController code:

[code]#import “BNRWebViewController.h”

@interface BNRWebViewController ()

@property (nonatomic, strong) UIBarButtonItem *backButton;
@property (nonatomic, strong) UIBarButtonItem *forwardButton;
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation BNRWebViewController

  • (instancetype)init
    {
    self = [super init];
    if (self) {
    self.backButton = [[UIBarButtonItem alloc] initWithTitle:@“Back” style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    self.forwardButton = [[UIBarButtonItem alloc] initWithTitle:@“Forward” style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
    self.backButton.enabled = NO;
    self.forwardButton.enabled = NO;

      UINavigationItem *navItem = self.navigationItem;
      navItem.rightBarButtonItems = @[self.forwardButton, self.backButton];
    

    }
    return self;
    }

  • (void)loadView
    {
    self.webView = [[UIWebView alloc] init];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.view = self.webView;
    }

  • (void)goBack: (id)sender
    {
    [self.webView goBack];
    }

  • (void)goForward: (id)sender
    {
    [self.webView goForward];
    }

#pragma mark - UIWebView delegate

  • (void)webViewDidFinishLoad:(UIWebView *)webView
    {
    if (webView.canGoBack) {
    self.backButton.enabled = YES;
    } else {
    self.backButton.enabled = NO;
    }

    if (webView.canGoForward) {
    self.forwardButton.enabled = YES;
    } else {
    self.forwardButton.enabled = NO;
    }
    }

#pragma mark - Property overrides

  • (void)setURL:(NSURL *)URL
    {
    _URL = URL;
    if (_URL) {
    NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
    [(UIWebView *)self.view loadRequest:req];
    }
    }

@end[/code]