2009-08-15 10 views
1

タブバーのアイテムをどのように識別できますか?タブバーの項目を識別する方法は?

私はこのようなNAvigationControllerが含まれているtabBarControllerを持っている:

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

各navigationControllerはこの配列内にあります。この方法では

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

そしてI、すなわち:このよう

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

私はウィッヒタブバーの項目を識別私はこの方法で各タブバーの項目でアクションを管理


私はクリックします。

しかし、iphone画面でタブバーを編集することができます(タブバーを初期化する配列に6つのviewControllerがあるため)、そして私が使用している方法が間違っています私はこの編集ツールを使用するときにタブバー内のビューコントローラの位置。

おかげ

答えて

6

あなたはその比較、各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パラメータに対するそれらを比較することができます。

例:あなたは、あなたのUITabBarController上の編集を禁止することができます

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

(コントローラのcustomizableViewControllersプロパティに空の配列やnilを渡します)。

例:

​​
+0

1)私は、各UITabBarItemにUITabBarItemのタグを与えた場合私はtabbaritemをviewcontroller、rigthに関連づけていません。つまり、タブバーを編集すると、tabcontitemは見つかりますが、viewcontrollerは見つかりません。 2)これを行う方法 3)問題を解決してタブバーを編集したいので、これは私の最後の選択肢になります。 比較する方法は次のとおりです。 if(viewController == [self.tabBarController.viewControllers objectAtIndex:0]) このように変更する必要がありますか? ありがとう –

+0

@Miriam答えを編集しました –

0

しかし、私はこのようなViewControllersを作成しています。(その後、私はの#defineを作る、または別の名前を入れることはできません)

(UINavigationController *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(クラス) 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; 

}

これを行うには

(無効)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

、私は元素のデモで始まりました。このデモでは、各データソースには独自のオーバーライドされた名前が付いています。その後、私は特定のタブのために何かをする必要がある時は、(それが他のタブとは異なるデータソースを持っているので)、私のメインのナビゲーションコントローラに、私が行います

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

しかし、あなたが(それぞれのViewControllerが宣言されている)同じクラスにいる場合、 "datasource.name"を呼び出すことはできません。 この同じクラスを呼び出して比較を行うにはどうすればいいですか? ありがとう –

+0

ビデオTableViewController * tabbedViewController =(VideosTableViewController *)[[localViewControllersArray objectAtIndex:[WHERTVER]] topViewController]; if(tabbedViewController.dataSource.name == @ "Something"){ //何かを実行 } –

関連する問題