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

目标C:如何正确设置TabBarController的didSelectViewController方法,以便每次点击VC时都能刷新它

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

    试图完成

    轻触 tabbaritem 它将在 tabbaritem VC

    发行

    当我打开 tabbaritem2 它会召唤 didSelectViewController 然后是各自的方法。当我打开 tabbaritem3 它仍将调用 选择视图控制器 安定素3

    但当我重新打开 安定素2 . 它仍将调用 选择视图控制器 安定素3 而不是 安定素2 各自的方法都不起作用了

    The issue without break

    The issue with break

    问题

    如何正确设置 选择视图控制器 它将分别调用和加载方法吗?

    代码

    MyTabBarController.m(我需要在这里做些什么吗?)

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        NSLog(@"didSelectViewController... ");
    
       // if ([viewController isKindOfClass:[UINavigationController class]]) {
       //      [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
       // }
    
        //=== I tried the following but it is not loading the method=====================
        //if ([viewController isKindOfClass:[ClassNavigationController class]]) {   // Here newViewController is the controller where the webview reload happens.
          //  [[[Classes alloc] init] reloadWebViewData];  // We create and instance for the new controller and call the delegate method where the reload works.
        //}
    
        //if (viewController == [tabBarController.viewControllers objectAtIndex:2]){
         //   [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
         //   [[[Classes alloc] init] LoadClasses];
    
        //}else if (viewController == [tabBarController.viewControllers objectAtIndex:3]){
    
          //  [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
          //  [[[Gym alloc] init] handleRefreshGym:nil];
    
        //}else{
            //=== The following code will make viewWillAppear load on each tab bar item
            //=== Without it, tapping on new tab bar item will not load viewWillAppear
          //  [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        //}
        //=================================================================================
    }
    

    类.m

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        UITabBarController *tabBarController = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;
    
        [tabBarController setDelegate:self];
    
    }
    
    -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
        NSLog(@" Classes Called ");
    
        if (viewController == [tabBarController.viewControllers objectAtIndex:2])
        {
            [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
            [self handleRefresh:nil];
    
        }else{
            [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        }
    
    }
    

    健身房.m

        - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        UITabBarController *tabBarController1 = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;
    
        [tabBarController1 setDelegate:self];
    
    }
    
    -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
        NSLog(@" Gym Called ");
    
        if (viewController == [tabBarController.viewControllers objectAtIndex:3])
        {
            [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
            [self handleRefreshGym:nil];
    
        }else{
            [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        }
    
    }
    

    1

    1 回复  |  直到 6 年前
        1
  •  1
  •   R4N    6 年前

    FirstView控制器

    #import "FirstViewController.h"
    
    @interface FirstViewController () <UITabBarControllerDelegate>
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tabBarController.delegate = self;
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        NSLog(@"Who's my tab bar controller delegate = %@", self.tabBarController.delegate);
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        NSLog(@"Delegate called on %@", NSStringFromClass([self class]));
    }
    
    
    @end
    

    第二视图控制器

    #import "SecondViewController.h"
    
    @interface SecondViewController () <UITabBarControllerDelegate>
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tabBarController.delegate = self;
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        NSLog(@"Who's my tab bar controller delegate = %@", self.tabBarController.delegate);
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        NSLog(@"Delegate called on %@", NSStringFromClass([self class]));
    }
    
    @end
    

    如果我运行此操作并从选择FirstViewController的选项卡开始,然后选择SecondViewController的选项卡,然后返回到选择FirstViewController的选项卡这是我得到的日志结果:

    First Tab Selected:
    Who's my tab bar controller delegate = <FirstViewController: 0x7ff9eb406970>
    Delegate called on FirstViewController
    
    Second Tab Selected:
    Who's my tab bar controller delegate = <SecondViewController: 0x7fa33ac0a540>
    Delegate called on FirstViewController (this is still FirstViewController here because the tab bar selection occurred prior to setting the SecondViewController to the tabBarControllerDelegate)
    
    First Tab Selected:
    Who's my tab bar controller delegate = <SecondViewController: 0x7fa33ac0a540>
    Delegate called on SecondViewController
    
    Second Tab Selected:
    Who's my tab bar controller delegate = <SecondViewController: 0x7fa33ac0a540>
    Delegate called on SecondViewController
    
    ...
     and it continues on that the SecondViewController will remain the delegate
    

    所以我的建议是使用一种不同的模式,只维护一个协调器来处理TabBarDelegation。

    根据您对其他建议的评论进行编辑

    https://medium.com/ios-os-x-development/ios-tips-pull-to-refresh-in-less-than-30-seconds-ef884520f0df 如果您明确要求选项卡栏委托在每个视图控制器选择上执行某些操作,我建议使用一个中心对象作为唯一的选项卡栏委托,并让它根据通过delegate方法传入的视图控制器来处理要执行的任务 tabBarController:didSelectViewController: 再举一个例子。