![](/img/trans.png)
[英]How do I instantiate a storyboard with a given root viewcontroller and initial viewcontroller?
[英]How do I check present viewController or root viewController?
我已經使用UITabBarDelegate
設置了我的class
並實現了它的method
didSelectItem
以檢測何時按下某個tabBar
項目。 效果很好。 在每個tabBar
項目中,我有一個containerView
,如果用戶未登錄,它可以顯示“您必須登錄”頁面,另一個containerView
顯示嵌入在navigationController
viewControllers
。
我想跟蹤當前tab
項中顯示的viewController
和/或該tab
的root
viewController
。
我嘗試了許多不同的方法,但大多數都返回 nil 或者我無法讓它工作。 我認為整個container
情況使它更難處理。
它看起來像這樣:
@interface MyTabBarController () <UITabBarDelegate>
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
switch (indexOfTab) {
case 0: {
NSLog(@"🐳PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 1: {
NSLog(@"🐳PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 2: {
NSLog(@"🐳PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
//These return nil
NSLog(@"🐳AAAAAA %@", ((UINavigationController*)_appD.window.rootViewController).visibleViewController);
NSLog(@"🐳AAAAAA %@", ((UITabBarController*)_appD.window.rootViewController).selectedViewController);
NSLog(@"🐳AAAAAA %@", self.navigationController.topViewController);
NSLog(@"🐳AAAAAA %@", self.navigationController.visibleViewController);
//This returns with a value, but can't get it to work with conditionals, that is, when I'm in root, the else is triggered
NSLog(@"🐳AAAAAA %@", self.tabBar.window.rootViewController);
if(!self.tabBar.window.rootViewController) {
NSLog(@"🐳🐳🐳THIS IS NOT ROOT🐳🐳🐳");
}else {
NSLog(@"🐳🐳🐳this is ROOT🐳🐳🐳");
}
// This returns nil
((UINavigationController*)_appD.window.rootViewController).visibleViewController;
((UITabBarController*)_appD.window.rootViewController).selectedViewController;
//Doesn't work
if([self.navigationController.viewControllers[0] isKindOfClass:[ExperiencesListViewController class]]) {
NSLog(@"🐳IS KIND OF CLASS LIST");
}
if([self.navigationController.viewControllers[0].childViewControllers isKindOfClass:[ExperiencesContainerViewController class]]) {
NSLog(@"🐳IS KIND OF CLASS CONTAINER");
}
break;
}
case 3: {
NSLog(@"🐳PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 4: {
NSLog(@"🐳PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
default:
break;
}
}
So, what else can I try? Seems like I have to use `self.tabBar.window.rootViewController` in some way, no?
***EDIT***
Oh, and I have tried the `tabBarController` delegate but that doesn't trigger. Also, the `tabBar` is constructed programmatically if that helps.
很抱歉沒有正確閱讀您的問題。 這是我建議你做的。
您有興趣跟蹤的所有這些視圖控制器:您應該讓他們從-viewDidAppear:
或-viewWillAppear:
方法中發送自定義通知。 然后讓您的 ApolloTabBarController 對象注冊該通知。 當它收到通知時,您可以存儲對視圖控制器的引用。 該引用將始終指向活動視圖控制器。
在您的各個視圖控制器中,執行以下操作:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"XYZViewControllerDidBecomeActiveNotification"
object:self];
}
當然,您可能希望對通知名稱使用某種常量。
在您的 ApolloTabBarController 類中,注冊XYZViewControllerDidBecomeActiveNotification
並實現如下內容:
- (void)viewControllerDidBecomeActive:(NSNotification *)notification
{
self.activeViewController = [notification object];
}
我希望這有幫助!
當您為每個選項卡設置每個視圖控制器時,將UITabBarItem
的tag
屬性設置為對應於選項卡欄viewControllers
數組中視圖控制器的索引。
UIViewController* myFirstVC = [[UIViewController alloc] init];
UIViewController* mySecondVC = [[UIViewController alloc] init];
// "self" is your ApolloTabBarController.
[self setViewControllers:@[myFirstVC, mySecondVC]];
myFirstVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:@"First" image:nil tag:0];
mySecondVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:1];
然后,您將能夠獲取對視圖控制器的引用。
// In your example, your ApolloTabBarController acts as its own delegate.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
UIViewController* activeVC =
[[self viewControllers] objectAtIndex:[item tag]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.