12

對不起,新手問題。我在我的主窗口視圖中有一個UITabBar,並且每個Tab都有一個UINavigationControllers數組。該結構類似於iPod應用程序,因爲可以通過選擇TabBar項目來查看主視圖,然後用戶可以通過將視圖推送到堆棧,使用NavigationController進一步深入查看。以編程方式在Xcode中按下UITabBar按鈕

我想要做的就是做相當於從代碼中的任何子視圖底部按TabBar按鈕(即,更改TabBar的選定屬性,並顯示啓動第一個視圖控制器爲標籤)。

任何幫助將不勝感激。

戴夫

回答

23
[myTabBarController setSelectedIndex:index] 

編輯:從評論回答了部分問題2:

您可以定義AppDelegate中的方法用於切換到不同的選項卡。

而且你可以從任何地方獲得的appdelegate的保持和發送的短信.. 類似:

MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate]; 
[appDelegate SwitchToTab:index] 
+0

對我來說,通過調用Tabbarcontroller方法來設置似乎是正確的!但會檢查它並不重要! – prakash 2009-11-25 08:34:16

+0

是的,也許你是對的 - 我拿了我的代碼的一部分,我用TabBar沒有tabbarcontroller – Vladimir 2009-11-25 08:37:13

+0

這是非常感謝。好吧,愚蠢的問題第2部分。我如何從我的代碼到根對象?實際的TabBar是從根視圖控制器的代碼中設置的。我想能夠設置TabBar的屬性,但是從另一個視圖控制器被推送到堆棧。我解釋得很好嗎?這就像我需要一個等同的自我,但對於樹頂部的父對象。 再次爲newbe問題感到抱歉,我錯過了我猜想中相當基本的東西。 – 2009-11-25 18:19:49

11

或者......

[self.parentViewController.tabBarController setSelectedIndex:3]; 
3

我想回復普拉卡什,但無法弄清楚如何。也許我被阻擋直到我的分數上升。

無論如何,我希望這可以幫助別人:

我在做什麼普拉卡什說了,什麼也沒有發生。這是因爲,以獲得一個指向我的應用程序委託,我這樣做:

AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init]; 

當我本來應該這樣做:

AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate]; 

新手的錯誤。

0

對於這一點,你只需要採取UITabBar控制器 -

.H文件 -

UITabBarController *_pTabBarController; 
@property (nonatomic, retain) IBOutlet UITabBarController *_pTabBarController; 

.m File - 
// synthesize it 
@synthesize _pTabBarController;  

At initial load 

// You can write one function to add tabBar - 

// As you have already mentioned you have created an array , if not 

_pTabBarController = [[UITabBarController alloc] init]; 
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4]; 
    UINavigationController *theNavigationController; 

    _pController = [[Controller alloc] initStart]; 
    _pController.tabBarItem.tag = 1; 
    _pController.title = @"Baranches"; 
    theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController]; 
    theNavigationController.tabBarItem.tag = 1; 
    theNavigationController.tabBarItem.image = [UIImage imageNamed:@"icon_branches.png"]; 
    [localViewControllersArray addObject:theNavigationController]; 
    [theNavigationController release]; 

比可以設定指標,按您的需求

self._pTabBarController.selectedIndex = 0; // as per your requirements 
0
[self.parentViewController.tabBarController setSelectedIndex:3]; 

選擇對我來說這個指數,但它只是強調了navbarcontroller的指數作爲活躍指數,但它突出了我ndex它實際上是在tabbarmenu項目建議的不同視圖控制器上。

只是想補充一點,我使用了這個從我的視圖控制器,它執行像某人實際上按下menuitem;從代碼:

UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController; 
[MyTabController setSelectedIndex:1]; 

謝謝你的這篇文章/答案它幫助了我的項目很多。

0

我想做類似的事情,但對於XCode 6.4 iOS(8.4)setSelectedIndex本身不會這樣做。

標籤欄的視圖控制器添加到列表,然後使用類似的一些功能下面,然後調用它:

FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0]; 
[self.selectedViewController.view removeFromSuperview] 
[self.view insertSubview:firstVC.view belowSubview:self.tabBar]; 
[self.tabBar setSelectedItem:self.firstTabBarItem]; 
self.selectedViewController = firstVC; 

你可能有類似的代碼已經在您的didSelectedItem內..

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { 

    if (item == self.firstTabBarItem) 
     // Right here 
    } 
    else if ... 
} 
相關問題