xcode TabBarController didSelectViewController 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13707207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:14:46  来源:igfitidea点击:

TabBarController didSelectViewController not working

objective-ciosxcodeuitabbarcontrolleruitabbar

提问by user1256477

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

我知道这是一个非常重复的话题,但我无法让它发挥作用。

MainTab.h:

主选项卡.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.

我找不到我缺少的东西,任何帮助将不胜感激。

Now I try to link it into MainStoryBoard:

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

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

But it doesnt work, what are the connection?

但它不起作用,有什么联系?

回答by Rob

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

根据您的@interface(以及您随后的屏幕快照),MainTabUITabBarController,因此以下行:

self.tabBarController.delegate = (id)self;

Should just be:

应该只是:

self.delegate = self;

You don't want a tabBarControllerproperty in the UITabBarController, itself, nor do you want to use the self.tabBarControllersyntax. 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.

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



Thus, it work if MainBaris defined as:

因此,如果MainBar定义为:

//  MainBar.h

#import <UIKit/UIKit.h>

@interface MainBar : UITabBarController

@end

And

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

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

interface builder

界面生成器

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

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

outlets

网点