如何识别标签栏项目?
Posted
技术标签:
【中文标题】如何识别标签栏项目?【英文标题】:How to Identify tab bar items? 【发布时间】:2009-08-15 12:53:31 【问题描述】:我想知道如何识别标签栏中的项目?
我有一个包含 NAvigationController 的 tabBarController,如下所示:
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];
每个 navigationController 都在这个数组中。
我使用方法管理每个标签栏项中的操作:
- tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
而我在这个方法中,即:
if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])
像这样我可以识别我点击的标签栏项目。
但问题是您可以在 iphone 屏幕中编辑 Tabbar(因为数组中有 6 个 viewControllers 初始化了 tabbar)然后,我使用的方式不正确,因为我可以更改位置使用此编辑工具时标签栏中的视图控制器。
谢谢
【问题讨论】:
【参考方案1】:您可以使用UITabBarItem
的标签属性为每个UITabBarItem
赋予一个唯一的数字标识符,然后进行比较。
例子:
#define FirstViewController 1
#define SecondViewController 2
switch ([[viewController tabBarItem] tag])
case FirstViewController:
//the user selected your first view controller, no matter where it is on the tabbar
break;
case SecondViewController:
break;
... etc
您可以记住指向每个 navigationControllers
的指针,并将它们与 viewController
参数进行比较。
例子:
//during your initial setup of the tabBarController:
UIViewController * firstViewController = //The view controller in the first tab
UIViewController * secondViewController = //The view controller in the second tab
...
if (viewController == firstViewController)
...
else if (viewController == secondViewController)
...
您可以禁止对 UITabBarController
进行编辑(将空数组或 nil
传递给控制器的 customizableViewControllers
属性)。
例子:
[myTabBarController setCustomizableViewControllers:nil];
【讨论】:
1) 如果我给每个 UITabBarItem 一个 UITabBarItem 的标签,我不会将此 tabbaritem 与 viewcontroller 关联,对吗?我的意思是,如果我编辑 tabbar,那么我可以找到 tabbaritem,但不是视图控制器。 2)如何做到这一点? 3)这将是我的最后一个选择,因为我想解决问题,并允许编辑标签栏。我比较的方式是这样的: if (viewController == [self.tabBarController.viewControllers objectAtIndex:0]) 我应该这样改变吗?谢谢【参考方案2】:但是,我正在像这样创建 ViewController:(然后我不能创建#define,或者输入不同的名称)
(UINavigationController *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass
id<VideosDataSource,UITableViewDataSource> dataSource = [[datasourceClass alloc] init];
// create the VideosTableViewController and set the datasource
VideosTableViewController *theViewController;
theViewController = [[VideosTableViewController alloc] initWithDataSource:dataSource];
// create the navigation controller with the view controller
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
// before we return we can release the dataSource (it is now managed by the ElementsTableViewController instance
[dataSource release];
// and we can release the viewController because it is managed by the navigation controller
[theViewController release];
return theNavigationController;
(void)setupPortraitUserInterface
// a local navigation variable
// this is reused several times
UINavigationController *localNavigationController;
// Create a tabbar controller and an array to contain the view controllers
tabBarController = [[UITabBarController alloc] init];
// define a custom frame size for the entire tab bar controller that will be in the
// bottom half of the screen.
CGRect tabBarFrame;
tabBarFrame = CGRectMake(0, 0, 320, 460);
tabBarController.view.frame = tabBarFrame;
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];
// setup the 6 view controllers for the different data representations
// create the view controller and datasource for the VideosSortedBySuggestionsDataSource
// wrap it in a UINavigationController, and add that navigationController to the
// viewControllersArray array
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySuggestionsDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// repeat the process for the VideosSortedBySuggSearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySuggSearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// repeat the process for the VideosSortedByMostViewedDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedByMostViewedDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// repeat the process for the VideosSortedByTopRatedDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedByTopRatedDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// repeat the process for the VideosSortedBySearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// repeat the process for the VideosSortedBySearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];
// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];
// set the tab bar controller view controller array to the localViewControllersArray
tabBarController.viewControllers = localViewControllersArray;
// the localViewControllersArray data is now retained by the tabBarController
// so we can release this version
[localViewControllersArray release];
// set the window subview as the tab bar controller
[self.view addSubview:tabBarController.view];
【讨论】:
【参考方案3】:为此,我从 Elements 演示开始。在那个演示中,每个数据源都有自己的覆盖名称。然后,任何时候我需要为特定选项卡做某事(因为它的数据源与其他选项卡不同),在我的主导航控制器中,我会这样做:
if (datasource.name == @"Some name")
// something
【讨论】:
但是如果你在我解释过的同一个班级(每个 ViewController 都在哪里声明)你不能调用“datasource.name”因为不存在这个。你怎么能在同一个班级里打电话来进行比较?谢谢 VideosTableViewController * tabbedViewController = (VideosTableViewController *)[[localViewControllersArray objectAtIndex:[WHATEVER]] topViewController]; if (tabbedViewController.dataSource.name == @"Something") // 做点什么以上是关于如何识别标签栏项目?的主要内容,如果未能解决你的问题,请参考以下文章