简体   繁体   中英

Swift - UIWebview back button with NavigationBar Back Button

At the moment im using a button on my uiwebview to go back through each webpage that the user searches and its working great. But is there anyway to use the navigation bar back button to go back through previous webpages and when theres no more pages to go back to then open the previous view controller?

Heres the code im using to go back which is just connected to a UIButton.

// Back button events
func onBackButton_Clicked(sender: UIButton)
{
    if(webview.canGoBack)
    {
        webview.goBack()
    }
}

Any help would be great.

Adding to your code you could add an else which would run when there is no more web history and pop the view controller, which would go back to the previous view controller (if you have you view controllers in a 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 back button and use your custom button add this code in ViewDidLoad:

self.navigationItem.backBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action:  "onBackButton_Clicked:")

And change your method above from:

onBackButton_Clicked(sender: UIButton)

to:

onBackButton_Clicked(sender: UIBarButtonItem)

I would suggest to overload the back button function and check for history.

override func viewDidLoad {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
        self.navigationItem.leftBarButtonItem = newBackButton;
    }

    func back(sender: UIBarButtonItem) {
        if(webview.canGoBack) {
             webview.goBack()
        } else {
             self.navigationController.popViewController(animated:true)
        }
    }

You need to use the BackBarButton to 1. navigate back through your previous webpages 2. move back to the previous controller from the first loaded web page. The latter requirement seems to be satisfied with the BackButton on NavigationBar at the moment. What you have to do is to add a custom BackButton .

override func viewDidLoad()
{
    super.viewDidLoad()
    leftButton = UIButton(type: UIButtonType.Custom)
    leftButton.frame = CGRectMake(0, 0, 36, 36)
    leftButton.clipsToBounds = true
    leftButton.setImage(UIImage(named: "yourBackButton.png"), forState: .Normal) // add custom image
    leftButton.addTarget(self, action: "onBackButton_Clicked:", forControlEvents: UIControlEvents.TouchUpInside)
    let leftBarButton = UIBarButtonItem()
    leftBarButton.customView = leftButton
    self.navigationItem.leftBarButtonItem = leftBarButton
}

func onBackButton_Clicked(sender: UIButton)
{
    if(webview.canGoBack) {
        webview.goBack()
    }
    else {
        self.navigationController.popViewController(animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM