代码之家  ›  专栏  ›  技术社区  ›  Hanz Cheah

Objective-C:Tabbaritem点击->方法调用->但WebView未刷新

  •  0
  • Hanz Cheah  · 技术社区  · 6 年前

    努力实现

    当我点击tabbaritem(比如说#2)时,它将调用该方法并重新加载web视图。

    当我点击tabbaritem时,方法被调用,但是web视图没有重新加载。

    Did not load the web view

    如果我调用 VC 它自己。我可以设法重新加载web视图。只有在点击tabbaritem时调用它,它才不会重新加载web视图。

    代码

    mytabbar控制器.m

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
        NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
        NSLog(@"controller title: %@", viewController.title);
    
            if (viewController == [tabBarController.viewControllers objectAtIndex:2])
             {
            [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
            tabBarController.delegate = self;
             [[[Classes alloc] init] LoadClasses];
    
        }else if (viewController == [tabBarController.viewControllers objectAtIndex:3]){
    
            [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
            tabBarController.moreNavigationController.delegate = self;
            [[[Gym alloc] init] handleRefreshGym:nil];
    
    }else{
            [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        }
    
    }
    

    类.m

    - (void)LoadClasses {
    
        sURL = @"www.share-fitness.com/apps/class.asp?memCode=SF100012&dtpClass=13/09/2018&lang=EN&lat=37.785835&long=-122.406418&ver=1&plat=IOS"
    
        NSLog(@"The URL To be loaded %@", sURL);
    
        NSURL *url = [NSURL URLWithString:sURL];
        sRefresh = sURL;
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        [webView loadRequest:urlRequest];
        [webView setDelegate:(id<UIWebViewDelegate>)self];
    
        UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
        [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
        [webView.scrollView addSubview:refreshControl];
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   R4N    6 年前

    正如我在另一封回信中提到的 Objective-C: How to properly set didSelectViewController method for TabBarController, so I can refresh the VC everytime it is tapped

    也就是说,您发布的代码的问题是,您正在初始化TabBarControllerDelegate方法中类的新实例,因此该方法将在这个新实例上调用,而不是在TabBarController的视图控制器中显示/存在的实例上调用。具体来说,这两行正在初始化新实例:

    [[[Classes alloc] init] LoadClasses];
    [[[Gym alloc] init] handleRefreshGym:nil];
    

    相反,您应该找到已经存在的实例,并对其调用方法。

    我建议创建一个 ParentViewController 使用公共方法 - (void)doStuffWhenTabBarControllerSelects; -(无效)BBARC控制器选择时的剂量; ). 这样,在TabBarController的委托方法中,您就可以找到 父视图控制器 -(无效)BBARC控制器选择时的剂量; 方法。

    下面是我的意思的一个例子:

    ParentViewController.h:

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ParentViewController : UIViewController
    - (void)doStuffWhenTabBarControllerSelects;
    @end
    
    NS_ASSUME_NONNULL_END
    

    ParentViewController.m:

    #import "ParentViewController.h"
    
    @interface ParentViewController ()
    
    @end
    
    @implementation ParentViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)doStuffWhenTabBarControllerSelects {
        NSLog(@"Fallback implementation if this method isn't implemented by the child class");
    }
    
    @end
    

    #import <UIKit/UIKit.h>
    #import "ParentViewController.h"
    
    @interface FirstViewController : ParentViewController
    
    
    @end
    

    #import "FirstViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)doStuffWhenTabBarControllerSelects {
        NSLog(@"I'm doing stuff on the %@ when the tab bar controller delegate calls back to selection", NSStringFromClass([self class]));
    }
    
    
    @end
    

    SecondViewController.h:

    #import <UIKit/UIKit.h>
    #import "ParentViewController.h"
    
    @interface SecondViewController : ParentViewController
    
    
    @end
    

    SecondViewController.m:

    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)doStuffWhenTabBarControllerSelects {
        NSLog(@"I'm doing stuff on the %@ when the tab bar controller delegate calls back to selection", NSStringFromClass([self class]));
    }
    
    @end
    

    MyTabBarController.h:

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    #import "MyTabBarController.h"
    #import "ParentViewController.h"
    
    @implementation MyTabBarController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.delegate = self;
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        // since your view controllers are embedded in nav controllers, let's make sure we're getting a nav controller
        if ([viewController isKindOfClass:[UINavigationController class]]) {
            // we're expecting a nav controller so cast it to a nav here
            UINavigationController *navController = (UINavigationController *)viewController;
            // now grab the first view controller from that nav controller
            UIViewController *firstViewControllerInNav = navController.viewControllers.firstObject;
            // check to make sure it's what we're expecting (ParentViewController)
            if ([firstViewControllerInNav isKindOfClass:[ParentViewController class]]) {
                // cast it to our parent view controller class
                ParentViewController *viewControllerToCallMethodOnAfterSelection = (ParentViewController *)firstViewControllerInNav;
                [viewControllerToCallMethodOnAfterSelection doStuffWhenTabBarControllerSelects];
            }
        }
    }
    
    @end
    

    然后,当您在两个选项卡之间选择时,您将看到以下输出:

    I'm doing stuff on the FirstViewController when the tab bar controller delegate calls back to selection
    
    I'm doing stuff on the SecondViewController when the tab bar controller delegate calls back to selection
    

    我建议做一些额外的研究/阅读文档:

    这里有大量的初学者信息: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/DefiningClasses/DefiningClasses.html#//apple_ref/doc/uid/TP40011210-CH3-SW1

    UITABBARC控制器: https://developer.apple.com/documentation/uikit/uitabbarcontroller?language=objc

    UITABBARC控制器删除: https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate?language=objc

    另一个有用的提示是,在Xcode中,您可以按住option键并单击一些内容,以快速查看解释/文档

    Documentation Quick Look

    /*!
     UITabBarController manages a button bar and transition view, for an application with multiple top-level modes.
    
     To use in your application, add its view to the view hierarchy, then add top-level view controllers in order.
     Most clients will not need to subclass UITabBarController.
    
     If more than five view controllers are added to a tab bar controller, only the first four will display.
     The rest will be accessible under an automatically generated More item.
    
     UITabBarController is rotatable if all of its view controllers are rotatable.
     */
    
    NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
    

    在“帮助”菜单下还有“开发人员文档”(CMD+SHIFT+0),其中包含大量有用的信息。