从Web视图卸载数据

时间:2013-08-12 13:42:56

标签: ios uiwebview

我的应用程序显示了一个表格视图,其中包含RSS源中的文章列表。当用户选择一行时,它会打开一个Web视图控制器来显示该文章。一切正常。我唯一的问题是,当用户返回到表视图并选择不同的文章时,Web视图最初会显示旧文章,直到加载新文章。如何从Web视图中删除旧数据?

这是我在tableview中的didSelectRowAtIndexPath方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:webViewController animated:NO];

    // Grab the selected item
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];

    // Construct a URL with the link string of the item
    NSURL *url = [NSURL URLWithString:[entry link]];

    // Construct a request object with that URL
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Load the request into the web view
    [[webViewController webView]loadRequest:req];
    webViewController.hackyURL = url;
}

这是Web视图控制器:

#import "WebViewController.h"
#import "TUSafariActivity.h"
#import "SVProgressHUD.h"

@implementation WebViewController
@synthesize webView=webView, hackyURL=hackyURL;

- (void)loadView
{
    // Create an instance of UIWebView as large as the screen
    CGRect screenFrame = [[UIScreen mainScreen]applicationFrame];
    UIWebView *wv = [[UIWebView alloc]initWithFrame:screenFrame];
    webView = wv;
    NSLog(@"%@",webView.request.URL);
    // Tell web view to scale web content to fit within bounds of webview
    [wv setScalesPageToFit:YES];

    [self setView:wv];
}

- (UIWebView *)webView
{ 
    return (UIWebView *)[self view];
}

- (void) showMenu
{
    NSURL *urlToShare = hackyURL;
    NSArray *activityItems = @[urlToShare];
    TUSafariActivity *activity = [[TUSafariActivity alloc] init];

    __block UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:@[activity]];
    activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];

    [self presentViewController:activityVC animated:YES completion:^{activityVC.excludedActivityTypes = nil; activityVC = nil;}];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)];
    self.navigationItem.rightBarButtonItem = systemAction;
}

@end

2 个答案:

答案 0 :(得分:4)

您可以通过将空白文档作为didSelectRowAtIndexPath的第一行加载来清除Web视图:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[webViewController webView] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

    ...

答案 1 :(得分:0)

最好在“Tableview did select”方法中维护一个标记值,并在Web视图加载视图控制器中保持相同的标记值。