ios UIWebView 上的后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16704950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Back Button on UIWebView
提问by Henry F
I'm trying to figure out how to create a back button that allows the user to go back one page. I read through Apple's Docs (which still go way over my head) and found out that I need to setup the canGoBack
and goBack
's. I have tried this, but for some reason it is not working. My UIWebView
is named viewWeb
, and I created and attached an outlet to my Back button, named backButton
, and also tagged it as 1. Here is my code that I wrote in the View Controller:
我试图弄清楚如何创建一个允许用户返回一页的后退按钮。我通读了 Apple 的 Docs(它仍然让我无法理解)并发现我需要设置canGoBack
和goBack
。我试过这个,但由于某种原因它不起作用。我UIWebView
的名字是viewWeb
,我创建了一个出口并将其附加到我的后退按钮,命名为backButton
,并将其标记为 1。这是我在视图控制器中编写的代码:
// Back button:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[_backButton addTarget:_viewWeb
action:@selector(goBack)
forControlEvents:UIControlEventTouchDown];
if ([_viewWeb canGoBack]) {
NSLog(@"Back button pressed.");
[_viewWeb goBack];
}
}
else return;
}
Does anyone know what I need to change / add to get this working?
有谁知道我需要更改/添加什么才能使其正常工作?
回答by Leehro
actionSheet:clickedButtonAtIndex:
is for UIActionSheet
objects, not UIButton
actions.
actionSheet:clickedButtonAtIndex:
用于UIActionSheet
对象,而不是UIButton
动作。
You should probably write an IBAction
method that looks something like this:
您可能应该编写一个如下所示的IBAction
方法:
- (IBAction)backButtonTapped:(id)sender {
[_viewWeb goBack];
}
and connect it to the Touch Up Insideaction from the button.
并将其从按钮连接到Touch Up Inside操作。
You can search for more info on IBAction
but that's likely what you want
您可以搜索更多信息,IBAction
但这可能是您想要的
回答by Talha Rasool
For swift 4 and swift 5
对于 swift 4 和 swift 5
@IBAction func refreshAction(_ sender: Any) {
self.webView.reload()
}
@IBAction func backAction(_ sender: Any) {
if(self.webView.canGoBack) {
self.webView.goBack()
}
}
@IBAction func forwardAction(_ sender: Any) {
if self.webView.canGoForward{
self.webView.goForward()
}
}
回答by Alsh compiler
I think it should be more specific like this
我认为它应该像这样更具体
- (IBAction)backButtonTapped:(id)sender {
if ([_viewWeb canGoBack] ) {
[_viewWeb goBack];
}
}
回答by Dr.Ax
I have struggled to get a working code for this function written in swift and finally here's what i came up with.
我一直在努力为这个函数快速编写一个工作代码,最后这就是我想出的。
@IBOutlet weak var goBackBtn: UIBarButtonItem!
@IBOutlet weak var goForwardBtn: UIBarButtonItem!
@IBOutlet weak var itemWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL (string: "www.google.com")
let requestObj = NSURLRequest(URL: url)
itemWebView.loadRequest(requestObj)
itemWebView.delegate = self
goBackBtn.enabled = false
goForwardBtn.enabled = false
}
func webViewDidFinishLoad(webView: UIWebView) {
goBackBtn.enabled = itemWebView.canGoBack
goForwardBtn.enabled = itemWebView.canGoForward
}
@IBAction func forward(sender: AnyObject) {
itemWebView.goForward()
}
@IBAction func back(sender: AnyObject) {
itemWebView.goBack()
}