2009-08-15 9 views
1

Ich würde gerne wissen, wie kann ich die Elemente in der Tableiste identifizieren?Wie identifizieren Tab-Tab-Elemente?

Ich habe einen TabBarController die Navigation wie folgt enthalten:

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

Jede Navigation innerhalb dieses Arrays ist.


verwalte ich die Aktionen in den einzelnen Tab-Leiste Element mit der Methode:

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

Und ich in diesem Verfahren, das heißt:

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

Mag ich identifizieren wich Tableiste Artikel Ich klicke auf.

ABER das Problem ist, dass Sie die Tabbar im iphone Bildschirm bearbeiten können (weil es 6 viewControllers im Array gibt, die die Tableiste initialisieren) und dann ist die Art, die ich benutze, falsch, weil ich das ändern kann Position der Viewcontroller in der Tableiste, wenn ich dieses Bearbeitungswerkzeug verwende.

Dank

Antwort

6

Sie können die UITabBarItem ‚s Tag-Eigenschaft verwenden, um jede UITabBarItem eine eindeutige numerische Kennung zu geben, vergleichen, dann das.

Beispiel:

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

Sie können Zeiger auf jede Ihrer navigationControllers erinnern und diejenigen, gegen die viewController Parameter vergleichen.

Beispiel:

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

Sie Bearbeiten auf der UITabBarController nicht zulassen kann (mit der ein leeres Array oder nil an die customizableViewControllers Eigenschaft des Controllers).

Beispiel:

[myTabBarController setCustomizableViewControllers:nil]; 
+0

1) Wenn ich ein UITabBarItem des Tags zu jedem UITabBarItem geben , Ich verbinde dieses Tabbaritem nicht mit dem Viewcontroller, richtig ?, ich meine, wenn ich die Tabbar bearbeite, dann kann ich das Tabaritem finden, aber nicht den Viewcontroller. 2) Wie geht das? 3) Dies wird meine letzte Option sein, weil ich das Problem lösen möchte, und erlauben Sie die Registerkarte bearbeiten. Die Art, die ich vergleiche ist wie folgt: if (viewController == [self.tabBarController.viewControllers objectAtIndex: 0]) Sollte ich diesen Weg ändern? –

+0

@Miriam Bearbeitete die Antwort w/weitere Informationen –

0

Aber, ich bin die Viewcontrollers wie folgt zu erstellen: (dann kann ich nicht #define machen, oder setzen verschiedene Namen)

(UINavigationController *) createNavigationControllerWrappingViewControllerForDataSourceOfClass: (Klasse) 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]; 

}

0

Um dies zu tun, begann ich mit den Elementen Demo ab. In dieser Demo hat jede Datenquelle ihren eigenen überschriebenen Namen.Dann habe ich zu jeder Zeit brauchen, um etwas für eine bestimmte Registerkarte zu tun (weil es eine andere Datenquelle als andere Tabs bekommen hat), in meinem Haupt-Navigation-Controller, das tue ich:

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

Aber wenn Sie in der gleichen Klasse sind, die ich Ihnen erklärte (wo jeder ViewController deklariert wird), können Sie "datasource.name" nicht cos aufrufen, das existiert nicht. Wie können Sie diese Klasse aufrufen, um den Vergleich zu machen? Danke –

+0

VideosTableViewController * tabbedViewController = (VideosTableViewController *) [[localViewControllersArray ObjektAtIndex: [WHATEVER]] topViewController]; if (tabbedViewController.dataSource.name == @ "Etwas") { // etwas tun } –