标签栏单击委托 [英] tab bar click delegate

查看:46
本文介绍了标签栏单击委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器(FirstViewController和SecondViewController)和一个选项卡栏控制器,我正在使用情节提要.在FirstViewController中,用户可以拖放图像视图.因此,每次用户单击第二个显示SecondViewController的TabBarItem时,我想检查用户是否每次单击TabBarItem都已删除图像.

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.

因此,我知道可以使用UITabBarDelegate及其方法-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item完成此操作.但是我做错了事情,因为没有调用该方法,并且我相信这是因为我无法正确设置委托.因此,我希望SecondViewController成为TabBarController的委托.

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.

所以在我的SecondViewController.h中,我有以下内容

So in my SecondViewController.h I have the following

@interface SecondViewController : UIViewController<UITabBarDelegate>

SecondViewController.m中,我有

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

}

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

但是什么也没有发生,设置委托时,我还会收到编译器警告:从不兼容的类型'SecondViewController * const __strong'分配给'id'

But nothing happens and when setting the delegate I also get a compiler warning: Assigning to 'id' from incompatible type 'SecondViewController *const __strong'

请对我好一点,这是我的第一个应用程序,也是我第一次尝试使用委托.

Please be gentle with me, this is my first app and the first time I'm trying to use delegates.

推荐答案

将以下代码添加到任何视图控制器

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

这篇关于标签栏单击委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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