在UIWebView上刷新?

时间:2015-11-30 10:22:25

标签: ios swift uiwebview

我试图在我加载网站的UIWebView上刷新。 UIWebView的代码非常简单

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var WebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let URL = NSURL(string: "https://the url/")

        WebView.loadRequest(NSURLRequest(URL: URL!))

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }    
}

有没有人有任何例子,因为无论我搜索什么,我都发现只需刷新桌面视图,我也不确定它是否适用于这个。

感谢。

更新1.在Sohil的建议之后,代码就像这样

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var WebView: UIWebView!
    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let URL = NSURL(string: "https://iremember.gr/")

        WebView.loadRequest(NSURLRequest(URL: URL!))
    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

            if (scrollView.contentOffset.y < 0){
                //reach top
                print("Reach Top")
                WebView.reload()
            }
            else {
                print("nothing")
            }
        }
    }

}

但它甚至不打印&#34;没有&#34;当我尝试重装时。

2 个答案:

答案 0 :(得分:8)

您可以使用UIScrollView来完成该任务。首先,您需要检查用户是否正在提取UIWebView&#39; Up&#39;或者&#39; Down&#39;,以下代码将有助于查看然后重新加载UIWebView

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

     if (scrollView.contentOffset.y < 0){
     //reach top
          print("Reach Top")
          webView.reload()
     }
 }

确保您已将代理人添加到UIScrollView

webView.scrollView.delegate = self

希望这有帮助!

答案 1 :(得分:0)

您好,您可以

 - (void)viewDidLoad {
 webViewHome.scrollView.delegate = self;
    // Refresh UIControl
    refreshControl = [[UIRefreshControl alloc]init];
    [webViewHome.scrollView addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refreshWebview) forControlEvents:UIControlEventValueChanged];

}

Refresh method 
- (void)refreshWebview {
    //TODO: refresh your data
    [refreshControl endRefreshing];
   [self loadWebviewWithUrl:kLoginUrl];
}

 I add refresh control in Did Load method. then I define refresh controller method and in that, I reload my webview.