首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何识别选项卡栏项目?

如何识别选项卡栏项目?
EN

Stack Overflow用户
提问于 2009-08-15 12:53:31
回答 3查看 3.6K关注 0票数 1

我想知道如何识别标签栏中的项目?

我有一个tabBarController,它包含这样的NAvigationController:

代码语言:javascript
运行
复制
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];

每个navigationController都在这个数组中。

我使用以下方法管理每个选项卡项中的操作:

代码语言:javascript
运行
复制
- tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController

我采用这种方法,即:

代码语言:javascript
运行
复制
if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])

就像这样,我识别了我点击的选项卡栏项目。

但问题是,您可以在iphone屏幕上编辑Tabbar (因为数组中有6 viewControllers来初始化选项卡),然后,我使用的方式是不正确的,因为当我使用此编辑工具时,可以更改选项卡中视图控制器的位置。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2009-08-15 14:27:01

您可以使用UITabBarItem的标记属性为每个UITabBarItem提供一个唯一的数字标识符,然后进行比较。

示例:

代码语言:javascript
运行
复制
#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参数进行比较。

示例:

代码语言:javascript
运行
复制
//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属性)。

示例:

代码语言:javascript
运行
复制
[myTabBarController setCustomizableViewControllers:nil];
票数 6
EN

Stack Overflow用户

发布于 2009-08-15 19:21:02

但是,我创建的ViewControllers如下:(然后我不能做#定义,或放置不同的名称)

( *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass {UINavigationController)

代码语言:javascript
运行
复制
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{

代码语言:javascript
运行
复制
// 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];

}

票数 0
EN

Stack Overflow用户

发布于 2009-08-16 21:42:07

为了做到这一点,我从元素演示开始。在该演示中,每个数据源都有自己的重写名称。然后,在我的主导航控制器中,每当我需要对特定的选项卡(因为它有一个与其他选项卡不同的数据源)做一些事情时,我都会这样做:

代码语言:javascript
运行
复制
if (datasource.name == @"Some name") {
    // something
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1281849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档