2

AppDelegateファイルの5つのタブすべてにタイトルを設定しました。アプリケーション内の別のビューでそのタイトルにアクセスしたいと思います。選択したタブの選択したインデックスをログウィンドウに出力する以下のコードがありますが、本当に取得したいのはそのタブのタイトルです。UITabBarControllerクラスリファレンスを調べましたが、これを可能にするものは何も見つかりませんでした。

私が避けようとしているのは、ある種のスイッチ、または別のファイルにすでに手動で設定した値をハードコードするif...elseステートメントです。

- (void)viewDidLoad {
[super viewDidLoad];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"The currently selected tab has an index of: %d", appDelegate.tabBarController.selectedIndex);

}

このコードは意図したとおりに機能します。タイトルを見るのが理想的です。

4

1 に答える 1

4
UIViewController* vc = appDelegate.tabBarController.selectedViewController;
NSString* tabTitle = vc.tabBarItem.title;

コードが選択したViewControllerにある場合は、さらに簡単です。

NSString* tabTitle = self.tabBarItem.title;
于 2012-08-29T17:24:13.510 に答える