14

I saw the UIRefreshControl in iOS 6 and my question is if it is possible to refresh a WebView by pulling it down and than let it pop up like in mail? Code I used rabih is the WebView:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [rabih addSubview:rabih];
4

8 に答える 8

9

Swiftでは、これを使用します。

WebView でプルして更新したいとします。

だから、このコードを試してください:

override func viewDidLoad() {
    super.viewDidLoad()
    addPullToRefreshToWebView()
}

func addPullToRefreshToWebView(){
    var refreshController:UIRefreshControl = UIRefreshControl()

    refreshController.bounds = CGRectMake(0, 50, refreshController.bounds.size.width, refreshController.bounds.size.height) // Change position of refresh view
    refreshController.addTarget(self, action: Selector("refreshWebView:"), forControlEvents: UIControlEvents.ValueChanged)
    refreshController.attributedTitle = NSAttributedString(string: "Pull down to refresh...")
    YourWebView.scrollView.addSubview(refreshController)

}

func refreshWebView(refresh:UIRefreshControl){
    YourWebView.reload()
    refresh.endRefreshing()
}

Objective-C では、これを使用しました。

- (void)addPullToRefreshToWebView{
    UIColor *whiteColor = [UIColor whiteColor];
    UIRefreshControl *refreshController = [UIRefreshControl new];
    NSString *string = @"Pull down to refresh...";
    NSDictionary *attributes = @{ NSForegroundColorAttributeName : whiteColor };
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
    refreshController.bounds = CGRectMake(0, 0, refreshController.bounds.size.width, refreshController.bounds.size.height);
    refreshController.attributedTitle = attributedString;
    [refreshController addTarget:self action:@selector(refreshWebView:) forControlEvents:UIControlEventValueChanged];
    [refreshController setTintColor:whiteColor];
    [self.webView.scrollView addSubview:refreshController];
}

- (void)refreshWebView:(UIRefreshControl*)refreshController{
    [self.webView reload];
    [refreshController endRefreshing];
}
于 2015-05-07T14:11:14.257 に答える
3

私は実際にそれを試してみましたが、次のエラーが発生しました。

*キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: 'UIRefreshControl は UITableViewController によってのみ管理される可能性があります'

ただし、UIScrollView と UICollectionView で使用できます。

于 2012-11-14T21:32:47.740 に答える
0

@umut-can-köseali の回答を拡張して、更新が終了したときに UIRefreshControl をより適切に制御できるようにします。

- (void)viewDidLoad {
    [super viewDidLoad];
    _refreshControl = [[UIRefreshControl alloc] init];
    [_refreshControl addTarget:self action:@selector(handleWebViewRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.webView.scrollView addSubview:_refreshControl]; //<- this is point to use. Add "scrollView" property.
}

- (void)webView:(UIWebView *)_webView didFailLoadWithError:(NSError *)error {
    [_refreshControl endRefreshing];
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    NSString *navigatorUrl = _webView.request.URL.absoluteString;
    if( navigatorUrl && ![navigatorUrl isEqualToString:@""]) {
        NSString *host=_webView.request.URL.host;
        NSString *path=_webView.request.URL.path;
        [_refreshControl endRefreshing];
    }
}
于 2015-08-31T14:30:50.303 に答える
0

必要に応じてカスタマイズできる場合があるCKRefreshControlをご覧ください。

于 2012-11-14T13:52:08.493 に答える
0

今のところ、そうではないと思います。実際には、UITableView だけでは使用できません。tableView を適切に使用するには、UITableViewController の一部である必要があります。

そうは言っても、UIWebView の上に 1 つ貼り付けて、そのフレームと値を手動で制御することで回避できる可能性があります。UITableViewController が更新され、その refreshControl プロパティで独自に実行されること。

于 2012-11-14T13:38:01.890 に答える