TabBarController didSelectViewController 不起作用 [英] TabBarController didSelectViewController not working

查看:27
本文介绍了TabBarController didSelectViewController 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常重复的主题,但我无法让它起作用.

I know this is a very repeat topic, but I can't get it works.

MainTab.h:

#import <UIKit/UIKit.h>

@interface MainTab : UITabBarController<UITabBarControllerDelegate, UITabBarDelegate> {

     IBOutlet UITabBarController *tabController;

}

@property (nonatomic,retain) IBOutlet UITabBarController *tabController;

@end

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);

}

我找不到我遗漏的东西,任何帮助将不胜感激.

I can't find what I'm missing, any help will be appreciated.

现在我尝试将其链接到 MainStoryBoard:

Now I try to link it into MainStoryBoard:

但是不行,是什么关系?

But it doesnt work, what are the connection?

推荐答案

根据您的 @interface(以及您随后的屏幕快照),MainTab@interfacecode>UITabBarController,所以下面这行:

On the basis of your @interface (and your subsequent screen snapshot), MainTab is the UITabBarController, so the following line:

self.tabBarController.delegate = (id)self;

应该是:

self.delegate = self;

您不想在 UITabBarController 本身中使用 tabBarController 属性,也不想使用 self.tabBarController 语法.如果您尝试从其子控制器之一引用选项卡栏控制器,则仅使用该语法.在标签栏控制器本身时,只需参考 self.

You don't want a tabBarController property in the UITabBarController, itself, nor do you want to use the self.tabBarController syntax. You only use that syntax if you're trying to reference the tab bar controller from one of its children controllers. When in the tab bar controller itself, just refer to self.

因此,如果 MainBar 定义为:

Thus, it work if MainBar is defined as:

//  MainBar.h

#import <UIKit/UIKit.h>

@interface MainBar : UITabBarController

@end

//  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

并且不要忘记设置标签栏控制器的类:

And don't forget to set the class of the tab bar controller:

连接检查器(我没有接触任何东西的地方)看起来像:

The connections inspector (where I didn't touch a thing) looks like:

这篇关于TabBarController didSelectViewController 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆