2011-02-26 50 views
9

我正在使用應用程序底部的2個UITabBarController按鈕處理廣播流應用程序。一個用於Live Streaming,另一個用於前40名單。在UITabBarController中查找活動標籤

我對這兩個選項卡使用了一個共同的StreamingViewController。但取決於選中的選項卡 - 需要將不同的url傳遞給StreamingViewController,以便它可以播放相應的Feed。

那麼我如何確定活動選項卡?

回答

37

你甚至看過文檔嗎? tabBarController.selectedIndex將爲您提供選定選項卡的索引,並且tabBarController.selectedViewController將爲您提供一個指向活動選項卡上的視圖控制器的指針。

+0

我試過了 - 但失敗了.. 這就是我在做的事情 - 基本上當在.m文件中的StreamingViewController.xib中按下「播放」按鈕時,我會檢查 如果(tabBarController.selectedIndex == 0){/ /加載url的實時流和播放音頻的網址} if(tabBarController.selectedIndex == 1){/ /加載URL Top40列表和播放音頻} 我試過用nslog打印selectedIndex的值,它總是顯示0. – Mohit 2011-02-26 16:15:13

+0

如果你已經嘗試過了,你應該在你的問題中這樣說。節省我們的工作,並給你一個更好的答案。你確認'tabBarController'是否有效? – 2011-02-26 16:26:35

+0

我很抱歉...如何檢查tabBarController是否有效? – Mohit 2011-02-26 16:44:36

1

我不確定您是從哪裏調用代碼來加載URL,但您可能需要在用戶選擇不同的選項卡時收到通知。重寫你的UITabBarController類,並實現didSelectViewController方法:

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

在這種方法中,你應該能夠適當的URL傳遞給控制器​​。

(我知道這個答案是有點晚了,但我希望它能幫助)

+0

thnx ...會試試這個 – Mohit 2011-03-31 19:18:53

1

所以,我偶然發現了這個問題,因爲我的應用程序所需的相同的功能。答案是:您可以通過tabBarController.tabBar.selectedItem.title找到活動選項卡,這會爲您提供活動選項卡的標題,或者您可以使用tabBarController.tabBar.selectedItem.tag如果您已將標籤分配給tabBar項目。希望它可以幫助別人拉他們的頭髮:P