如何在WebView(iOS 5)中使用pull来刷新?

时间:2012-07-18 20:17:04

标签: ios uiscrollview

如何在iOS 5中为UIWebView进行“拉动刷新”?我找不到教程。

我在Tab Bar应用程序中有一个WebView,我想像Twitter一样刷新WebView。

我在哪里可以找到完整的教程?

谢谢

3 个答案:

答案 0 :(得分:0)

我无法提供教程,也许我会写一个,但我认为这很简单, 我这样做稍微修改了SPPullView。

Here you can grab the files (SPPullView.m and SPPullView.h).

主UIViewController中的相关代码如下:

- (void)viewDidLoad
{   
    [super viewDidLoad];
    self.pullView = [[SPPullView alloc] initWithScrollView:self.webView.scrollView];
    [self.pullView setDelegate:self];
    [self.webView.scrollView addSubview:self.pullView];    
    [self.webView.scrollView setDelegate:self];
    for (UIView *subview in [self.webView.scrollView subviews] ) {
       subview.userInteractionEnabled = NO; // used to disable copy/paste, etc inside the webview
    }
    //[self doYourStuff];
}

//Called when the user pulls to refresh (this is when you should update your data)
- (void) pullViewShouldRefresh: (SPPullView *) view {
    [self updateYourStuff];
}

您还可以添加一些逻辑或属性以防止用户缩放,或者在缩放时暂时停用拉视图。

答案 1 :(得分:0)

答案 2 :(得分:0)

我写了这段代码并且工作正常。这可能会对你有帮助。

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSString *fullURL = URL you want to hit;
   NSURL *url = [NSURL URLWithString:fullURL];
   NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
   webView.delegate = (id)self;
   [webView loadRequest:requestObject];

   UIRefreshControl *refreshControlOnPull = [[UIRefreshControl alloc] init];
   [refreshControlOnPull addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
   [webView.scrollView refreshControlOnPull]; //<- this is point to use. Add "scrollView" property.
}

-(void)handleRefresh:(UIRefreshControl *)refresh {
   // Reload my data
   NSString *fullURL = The Url you want to hit;
   NSURL *url = [NSURL URLWithString:fullURL];
   NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
   [webView loadRequest:requestObject];
   [refresh endRefreshing];
}