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:
再举一个例子。