xcode 标签栏点击委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17096785/
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
tab bar click delegate
提问by flouwer
I have two view controllers (FirstViewController and SecondViewController) and a Tab Bar Controller and I'm using Storyboards. In the FirstViewController user can drag and drop an imageview. So every time a user clicks on the second TabBarItem which displays the SecondViewController I would like to check if the user has dropped the image or not every time she clicks the TabBarItem.
我有两个视图控制器(FirstViewController 和 SecondViewController)和一个 Tab Bar Controller,我正在使用 Storyboards。在 FirstViewController 中,用户可以拖放图像视图。因此,每次用户单击显示 SecondViewController 的第二个 TabBarItem 时,我想检查用户是否在每次单击 TabBarItem 时删除了图像。
So I understand that this can be done with UITabBarDelegate
and with its method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
. But I'm doing something wrong because the method isn't called and I believe this is because I can't set the delegate properly. So I want the SecondViewController to be the delegate for TabBarController.
所以我明白这可以UITabBarDelegate
用它的方法来完成-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
。但是我做错了,因为没有调用该方法,我相信这是因为我无法正确设置委托。所以我希望 SecondViewController 成为 TabBarController 的代表。
So in my SecondViewController.h
I have the following
所以在我的SecondViewController.h
我有以下
@interface SecondViewController : UIViewController<UITabBarDelegate>
And in SecondViewController.m
I have
在SecondViewController.m
我有
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"%@", item);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
But nothing happens and when setting the delegate I also get a compiler warning: Assigning to 'id' from incompatible type 'SecondViewController *const __strong'
但是什么也没发生,在设置委托时我也收到编译器警告:从不兼容的类型“SecondViewController *const __strong”分配给“id”
Please be gentle with me, this is my first app and the first time I'm trying to use delegates.
请对我温柔点,这是我的第一个应用程序,也是我第一次尝试使用委托。
回答by Shams Ahmed
Add the following code to any of the view controllers
将以下代码添加到任何视图控制器
UITabBarController *tabBarController = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;
[tabBarController setDelegate:self];
// add any delegates methods to your class
// 将任何委托方法添加到您的类中
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"%@", tabBarController);
}
回答by jamil
-(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
NSLog(@"%@", item);
}
For more detail check info
欲了解更多详细信息
Thanks
谢谢
回答by alvin
I imported and implemented the following. Hope it helps.
我导入并实现了以下内容。希望能帮助到你。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (_mainTab.selectedItem.tag == 1) {
NSLog(@"TAB 1");
}
else if (_mainTab.selectedItem.tag == 2) {
NSLog(@"TAB2");
}
else if (_mainTab.selectedItem.tag == 3)
{
NSLog(@"TAB3");
}
else
{
NSLog(@"TAB NOT WORKING");
}
}
回答by danypata
You are using the wrong delegate protocol UITabBarDelegate
is usually used for customizing the UITabBar
objects. You need to use UITabBarControllerDelegate
protocol in order to check if a tab is selected or customize the behavior of tabs.
您使用了错误的委托协议UITabBarDelegate
,通常用于自定义UITabBar
对象。您需要使用UITabBarControllerDelegate
协议来检查是否选择了选项卡或自定义选项卡的行为。
回答by Andrei Shender
You should implement UITabBarControllerDelegate protocol instead and use this delegates callback to track selection:
你应该实现 UITabBarControllerDelegate 协议并使用这个委托回调来跟踪选择:
tabBarController:didSelectViewController:
Next thing is, that you should initialize delegate before it will be called. ViewDidLoad will be called after tabbarcontroller will try to talk to delegate.
接下来是,您应该在调用之前初始化委托。ViewDidLoad 将在 tabbarcontroller 尝试与委托交谈后被调用。
回答by riik
In order to get rid of the compiler warning your SecondViewController
should conform to the UITabBarControllerDelegate
protocol instead of the UITabBarDelegate
protocol.
为了摆脱编译器警告,您SecondViewController
应该遵守UITabBarControllerDelegate
协议而不是UITabBarDelegate
协议。
@interface SecondViewController : UIViewController<UITabBarControllerDelegate>