每次按下 tabbar 时加载初始 webview

Load initial webview every time tabbar is pressed

已编辑

我的初始 webview 在按下标签栏时加载,然后当点击 webview 时,我调用 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 检测点击的 URLString

当字符串与我的目标字符串匹配时,我将调用 segue [self performSegueWithIdentifier:@"NewsDet" sender:self]; 并传递 URLString 并在另一个 ViewController

上加载详细信息页面

现在,当我点击另一个标签栏然后点击回到这个标签栏时。详细信息页面保留。我想刷新回初始页面。我该怎么做?

我是否需要用 tabviewcontroller 的子 class 创建一个新的 class?请帮忙

Gym.m

- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:@"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];

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

}

- (void)handleRefresh:(UIRefreshControl *)refresh {

NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

[webView loadRequest:urlRequest];
[refresh endRefreshing];
}

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:@"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
 
 }

GymDet.m

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
sURL = self.urlString;

NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}

基本上我有 5 个选项卡控制器。每个选项卡都有一个导航控制器。当我点击 say Tab-A 时,它将在 VC1 中加载初始 webview 然后当我点击 webview URL 时,它将导航到新的 VC say VC2 并加载详细页面。 现在,如果我点击 Tab-B 并返回 Tab-A。我希望它加载初始 webview 或 VC1。现在,我的程序总是停留在VC2.

在弄清楚你要做什么之后...

您的问题与网络视图或加载 URLs 无关。问题是您在 NavigationController 中导航到一个新的 VC,然后切换选项卡,然后切换回来,您仍然在 "pushed" VC.

A comment not related to the problem:

I am on Tab-A, viewing VC-A1... Via "normal" Navigation Controller action, VC-A2 slides-in from the right. Now I select Tab-B, and I see VC-B1. Now, I want to switch back to Tab-A, and I expect to see what was there: VC-A2... but instead, you want to send me back to VC-A1. That really doesn't follow Apple's interface guidelines, and can be very confusing to your users.

但是,要实现这一点,您需要继承 UITabBarController 并实现 didSelectViewController:

#import "MyTabBarViewController.h"

@interface MyTabBarViewController () <UITabBarControllerDelegate>

@end

@implementation MyTabBarViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
    }
}

@end

这会起作用,但它会 "reset" 每个选项卡中的任何导航控制器。您需要添加一些其他逻辑来处理 第一个选项卡。


(原答案)

viewDidLoad 仅在视图加载时调用一次。在选项卡之间切换不会 "re-load" 视图。

您可能想在 viewWillAppear 中重新加载原始 URL,因为每次切换回该选项卡时都会调用它。

虽然,您可能还想检查原始 URL 是否是当前显示的 - 如果不是,您可能只想重新加载它。

而且...你从不想调用[self viewWillAppear:YES]; -- 这是系统提供的通知。