Silver Challenge Complete


#1

It took me a while to figure this one out. After tweaking the code for a number of hours, I finally got it working. Here’s the solution:(hope this helps :mrgreen: )

BNRWebViewController.h

[code]#import <Foundation/Foundation.h>

@interface BNRWebViewController : UIViewController

@property (nonatomic)NSURL *URL;

-(void)goForwardNow:(id)sender;
-(void)goBackwardNow:(id)sender;

@end[/code]

BNRWebViewController.m

[code]#import “BNRWebViewController.h”

@interface BNRWebViewController()

//*ADDED
@property (nonatomic, strong)UIToolbar *toolbar;

//*ADDED
@property (nonatomic, strong)UIWebView *webView;

//*ADDED
@property (nonatomic, strong)UIBarButtonItem *goForward;
@property (nonatomic, strong)UIBarButtonItem *goBack;

@end

@implementation BNRWebViewController

-(void)loadView{

self.webView = [[UIWebView alloc]init];
self.webView.scalesPageToFit = YES;
self.view = self.webView;

[self createToolBar];

}
-(void)createToolBar{

//*ADDED
//Add two button to navigation bar -------------------

//NOTE:Must create NSMutableArray in order for toolbar to work


//NSLog(@"width:%f",[[UIScreen mainScreen]bounds].size.width);

self.toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.width,100)];

NSMutableArray *items = [[NSMutableArray alloc]init];


self.goForward = [[UIBarButtonItem alloc] initWithTitle:@"goForward>" style:UIBarButtonItemStylePlain target:self action:@selector(goForwardNow:)];

//self.navigationItem.rightBarButtonItem = goForward;

self.goBack = [[UIBarButtonItem alloc]initWithTitle:@"<goBack" style:UIBarButtonItemStylePlain target:self action:@selector(goBackwardNow:)];

[items addObject:self.goBack];
[items addObject:self.goForward];

[self.toolbar setItems:items animated:YES];
[self.view addSubview:self.toolbar];

//self.navigationItem.leftBarButtonItem = goBack;

//---------------------------------------------------

}
//*ADDED
-(void)goForwardNow:(id)sender{

//NSLog(@"goForwardNow");

[self.webView goForward];

}
//*ADDED
-(void)goBackwardNow:(id)sender{

NSLog(@"goBackwardNow");

[self.webView goBack];

}

//*ADDED
-(void)prepareViewsForOrientation:(UIInterfaceOrientation)orientation{

if(UIInterfaceOrientationIsLandscape(orientation)){

    self.goForward.enabled = NO;
    self.goBack.enabled = NO;
    
    NSLog(@"landscape-width:%f",[[UIScreen mainScreen]bounds].size.width);
    
    self.toolbar.frame = CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.height,100);
}
else{
    
    self.goForward.enabled = YES;
    self.goBack.enabled = YES;
    
    NSLog(@"portrait-width:%f",[[UIScreen mainScreen]bounds].size.width);
    
    self.toolbar.frame = CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.width,100);
    
}

}
//*ADDED
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

[self prepareViewsForOrientation:toInterfaceOrientation];

}

-(void)setURL:(NSURL *)URL{

_URL = URL;

if(_URL){

    NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
    [(UIWebView *)self.view loadRequest:req];


}

}
@end
[/code]