use*_*477 5 xcode objective-c uitabbarcontroller uitabbar ios
我知道这是一个非常重复的话题,但我无法理解它.
MainTab.h:
#import <UIKit/UIKit.h>
@interface MainTab : UITabBarController<UITabBarControllerDelegate, UITabBarDelegate> {
IBOutlet UITabBarController *tabController;
}
@property (nonatomic,retain) IBOutlet UITabBarController *tabController;
@end
Run Code Online (Sandbox Code Playgroud)
MainTab.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(@"main tab");
[super viewDidLoad];
self.tabBarController.delegate = (id)self;
[self setDelegate:self];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"selected %d",tabBarController.selectedIndex);
}
Run Code Online (Sandbox Code Playgroud)
我找不到我错过的东西,任何帮助将不胜感激.
现在我尝试将其链接到MainStoryBoard:


但它不起作用,有什么联系?
Rob*_*Rob 16
根据您的@interface(和您的后续屏幕快照),MainTab是UITabBarController,所以以下行:
self.tabBarController.delegate = (id)self;
Run Code Online (Sandbox Code Playgroud)
应该只是:
self.delegate = self;
Run Code Online (Sandbox Code Playgroud)
您不希望tabBarController在UITabBarController本身中使用属性,也不想使用self.tabBarController语法.如果您尝试从其中一个子控制器引用标签栏控制器,则只使用该语法.在标签栏控制器本身时,只需参考self.
因此,如果MainBar定义为:
// MainBar.h
#import <UIKit/UIKit.h>
@interface MainBar : UITabBarController
@end
Run Code Online (Sandbox Code Playgroud)
和
// MainBar.m
#import "MainBar.h"
@interface MainBar () <UITabBarControllerDelegate>
@end
@implementation MainBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"selected %d",tabBarController.selectedIndex);
}
@end
Run Code Online (Sandbox Code Playgroud)
并且不要忘记设置标签栏控制器的类:

连接检查员(我没有碰过的东西)看起来像:
