1

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];
}
Hanz Cheah
  • 761
  • 5
  • 15
  • 44
  • **Note:** Don't call `-[NSUSerDefaults synchronize]`. From [Apple's documentation](https://developer.apple.com/documentation/foundation/userdefaults/1414005-synchronize)… _"this method is unnecessary and shouldn't be used."_ – Ashley Mills Sep 05 '18 at 15:05

1 Answers1

1

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] 
}
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • I am sorry, i am quite a noob, can you elaborate a bit more please – Hanz Cheah Sep 04 '18 at 07:52
  • Sure, what do you want to know? – tryKuldeepTanwar Sep 04 '18 at 07:53
  • Actually I am trying to prevent the back button from `Gym Detail` to reload the `Gym` page. I want user to go back to where they scroll at `Gym` page. So how to actually prevent calling `viewDidAppear` I am also trying to reload the page when the tab bar item is tapped again to refresh the `webview` again. – Hanz Cheah Sep 04 '18 at 07:59
  • okay so why don't you call the method handleRefresh in viewwillappear? – tryKuldeepTanwar Sep 04 '18 at 08:03
  • Yes it work on the first because it is loading at viewDidLoad – Hanz Cheah Sep 04 '18 at 08:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179363/discussion-between-hanz-cheah-and-dreambegin). – Hanz Cheah Sep 04 '18 at 08:04
  • I don't want to call handleRefresh because I don't want the web view to load when the back button is called from the detail page – Hanz Cheah Sep 04 '18 at 08:47
  • No still not solve. I started another question, if you have time, please take a look. https://stackoverflow.com/questions/52293432/objective-c-how-to-properly-set-didselectviewcontroller-method-for-tabbarcontro/52299231#52299231 – Hanz Cheah Sep 13 '18 at 01:31