如何在ios中选择选项卡时在AppDelegate中重新加载webview

时间:2013-06-18 14:24:27

标签: ios

我是iOs开发的新手,我无法解决这个问题 当我选择uitab时,我如何重新加载uiwebview是我的代码:

- (void)tabBarController:(UITabBarController *)aTabBarController didSelectViewController:(UIViewController *)viewController
{

    if(self.tabBarController.selectedIndex == 0)
    {
        [[NSURLCache sharedURLCache] removeAllCachedResponses];

        BSHomeViewController *homeViewController = [[BSHomeViewController alloc] initWithNibName:nil bundle:nil];
        BSHomeViewController.webView = nil;

        [BSHomeViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HOMEPAGE]]];


    }
    [[NSUserDefaults standardUserDefaults] setInteger:self.tabBarController.selectedIndex forKey:@"BSSelectedTab"];
}

我在appdelegate中声明它但它不会重新加载。

请帮我解决这个问题。所有答案都非常感谢。

3 个答案:

答案 0 :(得分:0)

您需要将AppDelegate设置为tabbarcontroller的委托。

您可以在didFinishLaunchingWithOptions

中执行此操作
self.tabBarController.delegate = self;

我不确定这是否是正确的设计。

为什么不在BSHomeViewController的viewDidAppear中重新加载webview?

答案 1 :(得分:0)

上面的代码在您调用BSHomeViewController *homeViewController = [[BSHomeViewController alloc] initWithNibName:nil bundle:nil];时实例化BSHomeViewController的新实例,并且该视图从未被显示或推送以供用户查看。当UITabBar切换到选定的ViewController时,是否有理由不选择利用viewWillAppear或viewDidAppear?

编辑:这很丑,但它会起作用......

TabBarDelegate.m

- (void)tabBarController:(UITabBarController *)aTabBarController didSelectViewController:(UIViewController *)viewController
{

    if(self.tabBarController.selectedIndex == 0)
    {
            [viewController viewWillAppear:YES];
    }
    [[NSUserDefaults standardUserDefaults] setInteger:self.tabBarController.selectedIndex forKey:@"BSSelectedTab"];
}

BSHomeViewController.m

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HOMEPAGE]]];
}

答案 2 :(得分:0)

每次显示BSHomeViewController的视图时重新加载webview

在您的BSHomeViewController.m

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HOMEPAGE]]];
}