xcode tabBar didSelectItem 似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10946058/
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 00:32:10  来源:igfitidea点击:

tabBar didSelectItem seems not to be working

iphoneiosxcodeuitabbar

提问by Rowie Po

In my header file I have this:

在我的头文件中,我有这个:

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;

}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

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

@end

In my main file I have this:

在我的主文件中,我有这个:

@synthesize tabBarController;

-(void)viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.delegate = self;
    self.view = tabBarController.view;
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"rawr"); 
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [tabBarController release];
    [super dealloc];
}


@end

I have already connected my tabbarcontrolleras a delegate to my file's owner in interface builder, but it still never calls the didSelectItemmethod.

我已经tabbarcontroller在界面构建器中将my作为委托连接到我的文件所有者,但它仍然从未调用该didSelectItem方法。

Is there anything that I'm missing here?

有什么我在这里想念的吗?

I have already added tabBarController.delegate = self;and it still does not work.

我已经添加了tabBarController.delegate = self;,还是不行。

回答by idearibosome

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

This method is a delegate method for UITabBar, not UITabBarController, so

这个方法是UITabBar的委托方法,不是UITabBarController,所以

self.tabBarController.delegate = self;

will not work.

不管用。

Tab bar controller has its own UITabBar, but changing the delegate of a tab bar managed by a tab bar controller is not allowed, so just try UITabBarControllerDelegate method like this:

标签栏控制器有自己的 UITabBar,但是不允许更改标签栏控制器管理的标签栏的委托,因此只需尝试 UITabBarControllerDelegate 方法,如下所示:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

回答by iOSDev

You need to add this:

你需要添加这个:

tabbarcontroller.delegate = self;

回答by Sameera R.

Use UITabBarControllerDelegateinstead of UITabBarDelegateand
-tabBarController:didSelectViewController{}instead of tabBar:didSelectItem{}

使用UITabBarControllerDelegate代替UITabBarDelegate
-tabBarController:didSelectViewController{}代替tabBar:didSelectItem{}

Interface

界面

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

main file

主文件

@implementation TabBarController
    @synthesize tabBarController;

    /*other stuff*/
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        NSLog(@"rawr"); 
    }
    /*other stuff*/
@end

回答by Tal Shumski

You have synthesized the tab bar, so you now need to write: self.tabBarController.delegate = self;

你已经合成了标签栏,所以你现在需要写: self.tabBarController.delegate = self;

回答by Andrea

Just set the delegate for the tab bar. You can do it by setting self.tabBarController.delegate = self;or using Interface builder do to the tabbarcontroller object (not bar) see the connection inspector and drag the connection on the file's owner.

只需为标签栏设置委托。您可以通过self.tabBarController.delegate = self;对 tabbarcontroller 对象(不是 bar)设置或使用 Interface builder do 来实现,查看连接检查器并拖动文件所有者上的连接。

回答by Ray Fix

There are many reasons, as indicated above, why it might not be working. Here is a more subtle reason. If you are creating your UI programatically (no storyboard or Xib) you need to set the screen of your UIWindow.

如上所述,它可能不起作用的原因有很多。这是一个更微妙的原因。如果您以编程方式创建 UI(没有故事板或 Xib),则需要设置 UIWindow 的屏幕。

self.window = [[UIWindow alloc] init];
self.window.screen = [UIScreen mainScreen];  // <- The UITabViewController will not
                                             //    accept taps without this.

If you are using a Xib, I believe this is equivalent to having "Full Screen at Launch" selected. That has to be checked or I have noticed my Tabs don't work.

如果您使用的是 Xib,我相信这相当于选择了“启动时全屏”。必须检查,否则我注意到我的标签不起作用。

Update:Whatever, down-vote me if you must but I had a non-working tab controller and this is what I had to do to get it working. None of the other answers on this page helped me. I suppose using a xib/storyboard is pretty rare these days (I think this was from iOS 5 days) but leaving here for the person that does and stumbles into this situation.

更新:无论如何,如果必须的话,请给我投反对票,但我有一个无法正常工作的选项卡控制器,这是我必须做的才能使其正常工作。此页面上的其他答案都没有帮助我。我想这些天使用 xib/storyboard 非常罕见(我认为这是从 iOS 5 天开始的)但是离开这里的人会遇到这种情况。