xcode AppDelegate 中的 UITabBarDelegate

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

UITabBarDelegate in AppDelegate

iphoneiosxcodeinterface-builderuitabbarcontroller

提问by Echilon

I have a UITabController in my main window, and would like to add some logic when each tab is selected. I've added the delegate to the header file:

我的主窗口中有一个 UITabController,并希望在选择每个选项卡时添加一些逻辑。我已将委托添加到头文件中:

@interface MyAppAppDelegate : NSObject <UIApplicationDelegate, UITabBarDelegate> {

I have a method for the tab change event:

我有一个选项卡更改事件的方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    //some code
}

But the code inside the didSelectItem method isn't called. I've connected the delegate for the UITabBarController to my AppDelegate in IB. Is there anything else I need to do?

但是没有调用 didSelectItem 方法中的代码。我已将 UITabBarController 的委托连接到 IB 中的 AppDelegate。还有什么我需要做的吗?

采纳答案by Echilon

I ended up putting it in the viewWillAppear method of the view in the specific tab I need. Seems to work fine.

我最终将它放在我需要的特定选项卡中视图的 viewWillAppear 方法中。似乎工作正常。

回答by sergio

If you assign the delegate via IB, you should connect the delegate for your TabBar (as opposed to your TabBarController) to your app delegate.

如果您通过 IB 分配委托,您应该将 TabBar(而不是 TabBarController)的委托连接到您的应用程序委托。

enter image description here

在此处输入图片说明

Indeed, you are not looking for the UITabBarControllerDelegate, but for the UITabBarDelegate.

实际上,您不是在寻找UITabBarControllerDelegate,而是在寻找UITabBarDelegate

If you do it programmatically, then, from your tab bar controller viewDidLoadexecute:

如果您以编程方式执行此操作,则从选项卡栏控制器viewDidLoad执行:

self.tabBar.delegate = [UIApplication sharedApplication].delegate;

回答by tarake

If you use UITabbarControlleryou can use UITabBarControllerDelegateinstead of UITabBarDelegate.

如果您使用UITabbarController,则可以使用UITabBarControllerDelegate代替UITabBarDelegate.

Then, you can set self.delegate = self. Then you use:

然后,您可以设置self.delegate = self. 然后你使用:

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

instead of:

代替:

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

回答by nevan king

Did you assign some class to be the tab bar's delegate? Something like

你分配了一些类作为标签栏的代表吗?就像是

myTabBar.delegate = self;

回答by Robin Rye

i might be out on a ledge here but i think the signature of the method should be:

我可能在这里的一个壁架上,但我认为该方法的签名应该是:

- (IBAction)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

and then you connect it to the tabbar in IB. after you change to IBAction it should appear in IB

然后将其连接到 IB 中的选项卡栏。更改为 IBAction 后,它应该出现在 IB 中