我正在开发一个基于 UITabbar 和视图层次结构的应用程序,如下所示。
UITabBarController ----> UINavigationController ----> UIViewController
我需要从 UIIVewController 访问 UITabBarController 。但以下属性总是返回 nil。
self.tabBarController 和 self.navigationController.tabBarController
有没有办法直接从子 viewController 访问 Tabbarcontroller 而不使用 AppDelegate ?
@implementation HomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"Home";
self.navigationItem.title = @"Home";
self.tabBarItem.image = [UIImage imageNamed:@"TabBarHome"];
UITabBarController *tab = self.tabBarController;
UITabBarController *tab1 = self.navigationController.tabBarController;
UITabBarController *tab2 = self.navigationController.presentingViewController;
}
return self;
}
对于您正在使用的层次结构:
我可以毫无问题地访问UITabBarController
来自ViewController
with:
self.tabBarController
将您的自定义初始化移至viewDidLoad
or viewDidAppear
然后你肯定可以访问TabBarController
with self.tabBarController
到达 TabBarController 的另一种方法是:
UITabBarController *tabBarController = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;
但在你的情况下这是完全没有必要的。
EDIT:
如果您正在使用 Xib,那么您已经在 AppDelegate 中以编程方式创建了一个 TabBarController。我确信你有类似的情况:
self.tabBarController = [[UITabBarController alloc] init];
然后你可以在 ViewController 中调用它:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = appDelegate.tabBarController;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)