如何识别标签栏项目?

发布于 2024-07-30 19:31:02 字数 713 浏览 3 评论 0原文

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

我有一个包含 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)然后,我使用的方式是不正确的,因为我可以改变viewcontrollers的位置当我使用此编辑工具时在选项卡栏中。

谢谢

I would like to know how can I identify the items in the tab bar?

I have a tabBarController that contain NAvigationController like this:

NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];

Each navigationController is inside this array.


I manage the actions in each tab bar item with the method:

- tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController

And I in this method, i.e.:

if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])

Like this i identify wich tab bar item i click on.

BUT the problem is that you can edit the Tabbar in the iphone screen (because there are 6 viewControllers in the array that initialize the tabbar) and then, the way that i'm using is incorrect,because i can change the position of the viewcontrollers in the tabbar when i use this edit tool.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-08-06 19:31:02

但是,我正在创建这样的 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];

}

But, I'm creating the ViewControllers like this: (then i cannot make #define, or put different names)

(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];

}

涫野音 2024-08-06 19:31:02

您可以使用 UITabBarItem 的 tag 属性为每个 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 传递给控制器​​的 customizedViewControllers 属性)。

例子:

[myTabBarController setCustomizableViewControllers:nil];

You can use the UITabBarItem's tag property to give each UITabBarItem a unique numerical identifier, then compare that.

Example:

#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
}

You can remember pointers to each of your navigationControllers and compare those against the viewController parameter.

Example:

//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) {
  ...
}

You can disallow editing on your UITabBarController (pass an empty array or nil to the controller's customizableViewControllers property).

Example:

[myTabBarController setCustomizableViewControllers:nil];
笔芯 2024-08-06 19:31:02

为此,我从 Elements 演示开始。 在该演示中,每个数据源都有自己的覆盖名称。 然后,每当我需要为特定选项卡执行某些操作(因为它具有与其他选项卡不同的数据源)时,在我的主导航控制器中,我会执行以下操作:

if (datasource.name == @"Some name") {
    // something
}

To do this, I started off with the Elements demo. In that demo, each datasource has it's own overridden name. Then any time I need to do something for a specific tab (because it's got a different datasource than other tabs), in my main navigation controller, I do:

if (datasource.name == @"Some name") {
    // something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文