正如我在另一封回信中提到的
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];
}
- (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];
}
- (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];
}
- (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 {
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController;
UIViewController *firstViewControllerInNav = navController.viewControllers.firstObject;
if ([firstViewControllerInNav isKindOfClass:[ParentViewController 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键并单击一些内容,以快速查看解释/文档
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
在“帮助”菜单下还有“开发人员文档”(CMD+SHIFT+0),其中包含大量有用的信息。