Iphone 如何识别选项卡栏项?
我想知道如何识别选项卡栏中的项目 我有一个包含如下NAvigationController的tabBarController:Iphone 如何识别选项卡栏项?,iphone,objective-c,uitabbarcontroller,Iphone,Objective C,Uitabbarcontroller,我想知道如何识别选项卡栏中的项目 我有一个包含如下NAvigationController的tabBarController: NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6]; 每个navigationController都在此阵列中 我使用以下方法管理每个选项卡栏项目中的操作: - tabBarController:(UITabBarController*)tab
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];
每个navigationController都在此阵列中
我使用以下方法管理每个选项卡栏项目中的操作:
- tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
在这种方法中,I,即:
if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])
像这样,我确定了我点击的标签栏项目
但问题是,您可以在iphone屏幕中编辑选项卡栏(因为数组中有6个ViewController初始化选项卡栏),然后,我使用的方式是不正确的,因为我可以在使用此编辑工具时更改选项卡栏中ViewController的位置
谢谢您可以使用
uitabaritem
的标记属性为每个uitabaritem
指定一个唯一的数字标识符,然后进行比较
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
您可以记住指向每个NavigationController
的指针,并将其与viewController
参数进行比较
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
您可以禁止对UITabBarController
进行编辑(将空数组或nil
传递给控制器的CustomizedLeviewController
属性)
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
您可以使用
uitabaritem
的标记属性为每个uitabaritem
指定一个唯一的数字标识符,然后进行比较
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
您可以记住指向每个NavigationController
的指针,并将其与viewController
参数进行比较
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
您可以禁止对UITabBarController
进行编辑(将空数组或nil
传递给控制器的CustomizedLeviewController
属性)
例如:
#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
}
//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) {
...
}
[myTabBarController setCustomizableViewControllers:nil];
但是,我创建的ViewController是这样的:(这样我就不能进行#定义,或者使用不同的名称) (UINavigationController*)CreateNavigationControllerRappingViewControllerForDataSourceOfClass:(类)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;
}但是,我创建的ViewController是这样的:(这样我就不能进行#定义,或者使用不同的名称) (UINavigationController*)CreateNavigationControllerRappingViewControllerForDataSourceOfClass:(类)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;
}要做到这一点,我从Elements演示开始。在该演示中,每个数据源都有自己的重写名称。然后,在我的主导航控制器中,每当我需要为特定选项卡执行某些操作时(因为它的数据源与其他选项卡不同),我都会执行以下操作:
if (datasource.name == @"Some name") {
// something
}
为此,我从Elements演示开始。在该演示中,每个数据源都有自己的重写名称。然后,在我的主导航控制器中,每当我需要为特定选项卡执行某些操作时(因为它的数据源与其他选项卡不同),我都会执行以下操作:
if (datasource.name == @"Some name") {
// something
}
1) 如果我给每个uitabaritem赋予一个uitabaritem的标记,我不会将这个tabbaritem与viewcontroller关联,rigth?,我的意思是,如果我编辑选项卡栏,那么我可以找到这个tabaritem,但找不到viewcontroller。2) 如何做到这一点?3) 这将是我的最后一个选项,因为我想解决这个问题,并允许编辑选项卡栏。我比较的方式如下:如果(viewController=[self.tabBarController.viewControllers objectAtIndex:0]),我应该这样更改吗?谢谢1)如果我给每个UITabBarItem一个UITabBarItem的标记,我不会将这个选项卡项与viewcontroller关联,rigth?,我的意思是,如果我编辑选项卡栏,那么我可以找到选项卡项,但找不到viewcontroller。2) 如何做到这一点?3) 这将是我的最后一个选项,因为我想解决这个问题,并允许编辑选项卡栏。我比较的方式如下:如果(viewController=[self.tabBarController.viewControllers objectAtIndex:0]),我应该这样更改吗?谢谢,但是如果你和我解释的是同一个类(其中声明了每个ViewController),你就不能调用“datasource.name”,因为这个类不存在。你怎么能调用同一个类来进行比较呢?感谢VIDEOSTABLEVEWCONTROLLER*选项卡VIDEOSTABLEVEWCONTROLLER=(VIDEOSTABLEVEWCONTROLLER*)[[localViewControllersArray objectAtIndex:[where]]topViewController];如果(tabbedViewController.dataSource.name=@“Something”){//do Something}但是如果您与我解释的同一个类(其中声明了每个ViewController),则不能调用“dataSource.name”,因为此类不存在。你怎么能调用同一个类来进行比较呢?感谢VIDEOSTABLEVEWCONTROLLER*选项卡VIDEOSTABLEVEWCONTROLLER=(VIDEOSTABLEVEWCONTROLLER*)[[localViewControllersArray objectAtIndex:[where]]topViewController];如果(tabbedViewController.dataSource.name=@“某物”){//do Something}