头文件:
#import <UIKit/UIKit.h>
@interface PFXTabControllerViewController : UITabBarController <UITabBarControllerDelegate>
@end
实现文件:
#import "PFXTabControllerViewController.h"
@interface PFXTabControllerViewController ()
@end
@implementation PFXTabControllerViewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"tabBar didSelectItem");
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"tabBarController didSelectViewController");
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self; // Tried it both with and without this.
}
显示UITabBarcontroller
很好,带有标签栏。单击选项卡会触发didSelectItem
,但不会触发didSelectViewController
,这是我需要处理的事件。UITabBarcontroller
正在加载并显示UIViewController
.
更新self.tabBarController.delegate = self
将该行更改为self.delegate = self
有效,并且都触发。我不明白为什么这是必要的。设置self.delegate = self
似乎......愚蠢。有任何想法吗?