Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert navigationcontroller's back button into browser's back button in iPhone?

I am showing a some text and images in UIwebview in navigation based application. It also contains some link to the source of the data i.e the websites. It is showing it in perfect manner. But when user click on the back button it pop the previous viewcontroller and goes to it. But, What I want when user click on the link the back button must be converted into browser's back button and act like it.

Any suggestions/sample code or tutorial for it?

like image 340
Sanchit Paurush Avatar asked Dec 01 '22 02:12

Sanchit Paurush


1 Answers

You can replace the navigation bar's back button with a browser back button whenever the UIWebView has the option to go back by doing something like this:

- (void)updateBackButton {
    if ([self.webView canGoBack]) {
        if (!self.navigationItem.leftBarButtonItem) {
            [self.navigationItem setHidesBackButton:YES animated:YES];
            UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)] autorelease];
            [self.navigationItem setLeftBarButtonItem:backItem animated:YES];
        }
    }
    else {
        [self.navigationItem setLeftBarButtonItem:nil animated:YES];
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self updateBackButton];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self updateBackButton];
}

- (void)backWasClicked:(id)sender {
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }
}
like image 137
cduhn Avatar answered Dec 07 '22 02:12

cduhn