My silver challenge


#1

The first part of the silver challenge was straight forward. I setup the toolbar using viewDidLoad and gave it actions that would go back or forward for each button ([_webView goBack] or [_webView goForward]). The bonus challenge wasn’t bad either. I figured it would be one of the delegate methods and once I checked I saw the following to. I set the back and forward buttons to be enabled based on if the webView returns yes or no for canGoBack or canGoForward.

[code]-(void)webViewDidStartLoad:(UIWebView *)webView
{
[_back setEnabled:[_webView canGoBack]];
[_forward setEnabled:[_webView canGoForward]];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[_back setEnabled:[_webView canGoBack]];
[_forward setEnabled:[_webView canGoForward]];
}
[/code]


#2

I figured I would add my viewDidAppear so others could see my toolbar. I didn’t do too much to it to make it look better because I was ready to move on to the next chapter and styling the toolbar is something we’ve done before and I am very familiar with it now.

[code]-(void)viewDidAppear:(BOOL)animated
{
CGRect rect = CGRectMake(0, self.view.frame.size.height -44, self.view.frame.size.width, 44);
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:rect];
_back = [[UIBarButtonItem alloc]initWithTitle:@“back” style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
_forward = [[UIBarButtonItem alloc]initWithTitle:@“forward” style:UIBarButtonItemStylePlain target:self action:@selector(forward:)];
NSArray *buttons = [NSArray arrayWithObjects:_back, _forward, nil];
[toolbar setItems:buttons];
[self.view addSubview:toolbar];
}

-(void)back:(id)sender
{
[_webView goBack];
}
-(void)forward:(id)sender
{
[_webView goForward];
}[/code]


#3

I found that using only webViewDidFinishLoad: allowed my buttons to be enabled properly. When I used webViewDidStartLoad: they would only enable after a couple of pages and wouldn’t always disable. I suppose it couldn’t hurt to have both though.

Also I created my toolbar in the loadView: method, which, as I understand it, is called before the view hierarchy is loaded into memory. And since the toolbar is a view, I figured it is part of the view hierarchy.

And finally, I used UIBarButtonItem constants to make toolbar buttons. Specifically, I used fastforward and rewind buttons. And I set their target to be self.view so that their action could be goBack or goForward and I didn’t need to implement additional methods just to call those methods.