xcode 如何以编程方式为 UIWebView 创建后退、前进和刷新按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3639411/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:42:08  来源:igfitidea点击:

How would I create back, forward, and refresh buttons for a UIWebView programmatically?

iphonexcodeuiwebview

提问by Linux world

I currently have a webview created but I do not want to use interface builder to create the back, forward, and refresh buttons. How would I create these buttons programmatically? I know how to create regular buttons with code, but as for webView delegate buttons, I am lost and have not been able to find many resources on it.

我目前创建了一个 webview,但我不想使用界面构建器来创建后退、前进和刷新按钮。我将如何以编程方式创建这些按钮?我知道如何使用代码创建常规按钮,但是对于 webView 委托按钮,我迷路了,并且无法在其上找到很多资源。

回答by e.James

From the UIWebViewdocumentation:

UIWebView文档:

If you allow the user to move back and forward through the webpage history, then you can use the goBackand goForwardmethods as actions for buttons. Use the canGoBackand canGoForwardproperties to disable the buttons when the user can't move in a direction.

如果您允许用户在网页历史记录中前后移动,那么您可以使用goBackgoForward方法作为按钮的操作。当用户无法沿某个方向移动时,使用canGoBackcanGoForward属性禁用按钮。

Setting up the buttons would then use addTarget:action:forControlEvents:(as pointed out by Sven):

然后将使用设置按钮addTarget:action:forControlEvents:(如Sven指出的那样):

[myBackButton addTarget:myWebView
                 action:@selector(goBack)
       forControlEvents:UIControlEventTouchDown];

If you want to get fancy and enable/disable the buttons based on the canGoBackand canGoForwardproperties, you will have to add some KVO notifications to your UIController.

如果您想根据canGoBackcanGoForward属性获得幻想并启用/禁用按钮,则必须向您的UIController.

回答by Sven

You need to set the target and action for the buttons using addTarget:action:forControlEvents:to your web view.

您需要为addTarget:action:forControlEvents:Web 视图使用的按钮设置目标和操作。

回答by Tyler Chong

To enable/disable Back or Forward button instead of using KVO, we can use the following "hack"

要启用/禁用后退或前进按钮而不是使用KVO,我们可以使用以下“ hack

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    if ([webView canGoBack])
        [backbutton setEnabled:YES];
    else
        [backbutton setEnabled:NO];

    if ([webView canGoForward])
        [fwdbutton setEnabled:YES];
    else
        [fwdbutton setEnabled:NO];
}

回答by Grant Isom

A simpler way to enable or disable buttons is:

启用或禁用按钮的更简单方法是:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [backButton setEnabled:webView.canGoBack];
   [fwdButton setEnabled:webView.canGoForward];
}