1
votes

I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.

Currently I have the following code

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
Note: Don't call -[NSUSerDefaults synchronize]. From Apple's documentation"this method is unnecessary and shouldn't be used."Ashley Mills

1 Answers

1
votes

First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check

if isUpdateRequire {
    [self handleRefresh:nil]
}

Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.

and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear

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