正如我在另一封回信中提到的
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键并单击一些内容,以快速查看解释/文档
/*!
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),其中包含大量有用的信息。