Silver Challenge Question


My webView only allows back button if I view 3 pages instead of 2 pages with allows back button to be used. Please help.

    [super viewDidLoad];
    self.forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"Forward" style:UIBarButtonItemStylePlain target:self action:@selector(forward)];
    self.backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    NSArray *buttonsArray = @[self.backButton,self.forwardButton];
    CGRect toolBarFrame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
    self.toolbar = [[UIToolbar alloc]initWithFrame:toolBarFrame];
    [self.toolbar setItems:buttonsArray animated:YES];
    [self.toolbar sizeToFit];
    [self.view addSubview:self.toolbar];
    NSDictionary *constraintDic = @{@"toolbar":self.toolbar};
    self.toolbar.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *toolbarLeftRightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[toolbar]-0-|" options:0 metrics:nil views:constraintDic];
    [self.view addConstraints:toolbarLeftRightConstraint];
    NSArray *toolbarVerticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[toolbar(==44)]-0-|" options:0 metrics:nil views:constraintDic];
    [self.view addConstraints:toolbarVerticalConstraint];

    [super viewWillAppear];

    if ([(UIWebView *)self.view canGoBack]){
        self.backButton.enabled = YES;
        NSLog(@"backButton YES");
        self.backButton.enabled = NO;
        NSLog(@"backButton NO");
    if ([(UIWebView *)self.view canGoForward]) {
        self.forwardButton.enabled = YES;
        NSLog(@"forwardButton YES");
        self.forwardButton.enabled = NO;
        NSLog(@"forwardButton NO");


    [(UIWebView *)self.view goForward];


    [(UIWebView *)self.view goBack];


I realize this was asked a long time ago but someone else might need an answer.

If you make the BNRWebViewController a delegate of UIWebView, you can implement a couple of methods that will be called as the web page is loaded. I was able to get my buttons to enable properly by setting their enabled property in the webViewDidFinishLoad: method.

Also, you can set self.view as the target of the button, and then set goForward or goBack as the action without having to implement a new method, since self.view is a UIWebView.

Nice work with the programmatic constraints btw.