2011-01-28 53 views
3

我有一個應用程序,我創建爲選項卡欄應用程序,並且有3個選項卡(主頁,搜索,我的帳戶)。主視圖加載一些信息和搜索按鈕。搜索按鈕然後將用戶帶到搜索視圖,但由於用戶沒有選擇標籤,所選標籤仍然是主頁標籤。一旦搜索viewDidLoad,我如何將所選標籤從主頁更改爲搜索?更改UITabBar選定標籤

回答

8

感謝您的幫助,你們幫我在我結束了使用的應用程序代理訪問的UITabBarController正確的方向。這就是它的樣子。

Motel_6AppDelegate *appDelegate = (Motel_6AppDelegate*) [[UIApplication sharedApplication] delegate]; 
    [appDelegate.rootController setSelectedIndex:1]; 
4

閱讀文檔。從搜索控制器內,您可以撥打:

self.tabBarController.selectedViewController = self; 

UITabBarController也有selectedIndex財產,如果這對你更方便。但是,viewDidLoad可能是放置此代碼的錯誤位置,因爲每次顯示搜索控制器時都可能不會調用它。您應該直接從用戶點擊主屏幕上的搜索按鈕時調用的操作內部選擇選項卡。

3

使用tabBarController.selectedIndex = intIndex; // inyour情況1

使用內部applicationDidLaunch這段代碼

0

這個片段對我的作品在iOS7和Xcode的5

- (IBAction)cancelButtonPressed:(id)sender 
{ 
    [self.tabBarController setSelectedIndex:1]; 
} 
故事板內