Objective c 目标C:如何为TabBarController正确设置didSelectViewController方法,以便每次点击VC时都能刷新它 努力完成

Objective c 目标C:如何为TabBarController正确设置didSelectViewController方法,以便每次点击VC时都能刷新它 努力完成,objective-c,uitabbarcontroller,uitabbar,uitabbaritem,Objective C,Uitabbarcontroller,Uitabbar,Uitabbaritem,轻触tabbaritem,它将在tabbaritem VC上调用相应的方法 问题 当我点击tabbaritem2时,它将调用tabbaritem2上的didSelectViewController,然后调用相应的方法。然后,当我点击tabbaritem3时,它仍然会调用tabbaritem3上的didSelectViewController和相应的方法 但当我切换回并点击tabbaritem2时。它仍将调用tabbaritem3上的didSelectViewController,而不是tabba

轻触tabbaritem,它将在tabbaritem VC上调用相应的方法

问题 当我点击tabbaritem2时,它将调用tabbaritem2上的didSelectViewController,然后调用相应的方法。然后,当我点击tabbaritem3时,它仍然会调用tabbaritem3上的didSelectViewController和相应的方法

但当我切换回并点击tabbaritem2时。它仍将调用tabbaritem3上的didSelectViewController,而不是tabbaritem2上的didSelectViewController,并且相应的方法不再工作

问题 如何正确设置didSelectViewController方法,以便在点击tabbaritem时分别调用和加载该方法

密码 MyTabBarController.m我需要在这里做些什么吗? 班级 健身房 神秘板
因此,TabBarController一次只能有一个委托。在您发布的代码中,您将在每个视图控制器的viewDidLoad生命周期方法中设置tabBarController.delegate=self,该方法在首次加载视图时调用一次。因此,无论最后一个视图控制器要加载什么,都将是最终的tabBarControllerDelegate

这里有一个非常简单的例子来说明我的意思:

FirstViewController

#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
第二视图控制器

#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

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


iOS中的一个相当标准的习惯用法是从服务器加载数据一次,通常在ViewDiLoad中加载相应的视图控制器,然后存储数据,然后使用pull to refresh控件,允许用户在命令上刷新数据:如果您明确要求tab bar委托对每个视图控制器选择执行某些操作,我建议使用一个中心对象作为唯一的选项卡栏委托,并让它根据通过委托方法传递的视图控制器来处理要执行的任务选项卡栏控制器:didSelectViewController:作为一个附加示例。

我是一个非常好的noob,你对其他模式或流程有什么建议吗?编辑了我的答案,加入了一些附加信息。
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