目标C-再次点击选项卡栏项时,如何重新加载Web视图?

时间:2018-09-04 07:18:00

标签: objective-c uiwebview uitabbarcontroller uirefreshcontrol

我真的需要帮助。最初,通过点击选项卡栏项,我将在webview用户上加载Gym VC,然后向下滚动web view 我要完成的工作是,当用户再次点击同一标签栏项时,webView将再次重新加载。我设法从handleRefresh调用了方法MyTabBarController,但是该方法根本没有执行任何操作。任何指导是非常感谢。

当前我有以下代码

MyTabBarController.m

#import "GymNavigationController.h"
#import "Gym.h"

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if([viewController isKindOfClass:[GymNavigationController class]]){

        Gym *myGym = [[Gym alloc] init];
        [myGym handleRefresh:nil];
     }
}

Gym.m

#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>

@interface Gym () {

AppDelegate *appDelegate;
NSString *sURL, *sRefresh; 

}

@end

@implementation Gym

@synthesize webView;

- (void)viewDidLoad {

    [super viewDidLoad];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    sURL = appDelegate.gURL;
    sURL = [sURL stringByAppendingString:@"/apps/gym.asp?"];

    NSLog(@" The sURL to be load for the current page : %@ ", sURL);

    sRefresh = sURL;

    [defaults setObject:sRefresh forKey:@"txtRefresh"];
    [defaults synchronize];

    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) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    //[self handleRefresh:nil];

    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

-(void)viewDidDisappear:(BOOL)animated{

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
}

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

    //--- Call the stored txtMemCode, something like session ---
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    sRefresh = [defaults objectForKey:@"txtRefresh"];

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

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

1 个答案:

答案 0 :(得分:1)

首先,我想建议您为什么每次单击选项卡栏时都要启动一个新的Gym对象,所以您应该获取Gym对象的引用,并对其进行初始化(如果该对象为nil,然后再对该引用进行处理),但这也许是您的要求如果是的话,我建议您创建一个BOOL变量,例如说isUpdateRequire,并在实例化视图控制器时分配isUpdateRequire = true。然后,在视图末尾是否已加载或视图将出现(根据您的需要)检查

if isUpdateRequire {
    [self handleRefresh:nil]
}

或者,您可以在webviewcontroller中创建协议,并将其委托分配给tabbarcontroller,并在需要时触发委托方法。

如果您不希望在返回vc时调用该方法,只需将此条件放入viewWillAppear

if self.isMovingToParentViewController { 
    [self handleRefresh:nil] 
}