我想知道如何识别标签栏中的项目?
我有一个tabBarController,它包含这样的NAvigationController:
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];每个navigationController都在这个数组中。
我使用以下方法管理每个选项卡项中的操作:
- tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController我采用这种方法,即:
if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])就像这样,我识别了我点击的选项卡栏项目。
但问题是,您可以在iphone屏幕上编辑Tabbar (因为数组中有6 viewControllers来初始化选项卡),然后,我使用的方式是不正确的,因为当我使用此编辑工具时,可以更改选项卡中视图控制器的位置。
谢谢
发布于 2009-08-15 14:27:01
您可以使用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];发布于 2009-08-15 19:21:02
但是,我创建的ViewControllers如下:(然后我不能做#定义,或放置不同的名称)
( *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass {UINavigationController)
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;}
(空)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];}
发布于 2009-08-16 21:42:07
为了做到这一点,我从元素演示开始。在该演示中,每个数据源都有自己的重写名称。然后,在我的主导航控制器中,每当我需要对特定的选项卡(因为它有一个与其他选项卡不同的数据源)做一些事情时,我都会这样做:
if (datasource.name == @"Some name") {
    // something
}https://stackoverflow.com/questions/1281849
复制相似问题