Adding to your code, you could add another that will work when there is no more web search history, and pull out the view controller, which will return to the previous view controller (if you see the controllers in the navigation controller):
if(webview.canGoBack) { //Go back in webview history webview.goBack() } else { //Pop view controller to preview view controller self.navigationController?.popViewControllerAnimated(true) }
Also, if you want to remove the default return button and use your custom button, add this code to ViewDidLoad:
self.navigationItem.backBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: "onBackButton_Clicked:")
And change your method above:
onBackButton_Clicked(sender: UIButton)
in
onBackButton_Clicked(sender: UIBarButtonItem)
source share