[英]Adding UITabBarItem to a UITabBar in a UIViewController
[英]Determining which UIViewController subclass that a UITabBar Item belongs to
我在appDelegate中實例化了UITabBarController,但是我需要一種方法來知道用戶何時按下了不同的選項卡欄項目(選項卡欄上的按鈕)。
-UITabBarDelegate協議進行救援(具有必需的didSelectViewController-方法)!
在Interface Builder中連接了所有東西之后,如何獲得對實際UIViewController子類實例的引用,該實例對應於已按下的此選項卡欄項目?
我需要此引用,因為每次按下選項卡欄項時,都需要在我的UIViewControllers子類中調用一個方法。
有什么建議么?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
可能是我無法正確理解您的問題,但似乎您要的只是傳遞給您提到的方法調用中的“ viewController”參數
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
UITabBarController也有一個屬性來獲取相同的信息
@property(nonatomic, assign) UIViewController *selectedViewController
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.