简体   繁体   English

带情节提要的iOS6中的UITabBarController子类

[英]Subclass UITabBarController in iOS6 with storyboards

I started a new project with iOS 6 ARC and Storyboards 我使用iOS 6 ARC和Storyboards开始了一个新项目

I made a very simple app that has a Tabbar and 2 views 我做了一个非常简单的应用,它具有一个Tabbar和2个视图

在此处输入图片说明

so now I created a TestViewController file with is extending UITabBarController and I put the custom class in the storyboard. 所以现在我创建了一个TestViewController文件,扩展了UITabBarController并将自定义类放在情节提要中。

now the issue is that I am trying to implement some delegate methods like 现在的问题是,我正在尝试实现一些委托方法,例如

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

But it never calls it. 但是它从来没有这样。 Can anyone help? 有人可以帮忙吗?

I noticed too that if I put that code in the first view controller it works fine. 我也注意到,如果将代码放在第一个视图控制器中,则可以正常工作。 It seems like the first view controller overwrites the TabBarController before. 似乎第一个视图控制器之前覆盖了TabBarController。 I am very confused. 我很困扰。

for more testing I added in the TestViewController.m some logging: 为了进行更多测试,我在TestViewController.m中添加了一些日志记录:

- (void)viewDidLoad { 
   [super viewDidLoad];
   // Do any additional setup after loading the view.
   NSLog(@"%@",self.tabBarController.viewControllers);
}

and the view controllers are null. 并且视图控制器为空。

I even tried doing self.tabBarController.delegate = self; 我什至尝试做self.tabBarController.delegate = self;

But still no solution. 但是仍然没有解决方案。

TestViewController is your tab bar controller, so your log should just be self.viewControllers, not self.tabBarController.viewControllers. TestViewController是您的标签栏控制器,因此您的日志应该只是self.viewControllers,而不是self.tabBarController.viewControllers。 You shouldn't need to add the property viewControllers either, since your subclass inherits that property from UITabBarController. 您也不需要添加属性viewControllers,因为您的子类从UITabBarController继承了该属性。 The reason your delegate message is not called is because of the same problem. 您的委托消息未被调用的原因是由于相同的问题。 You should set the delegate with (in TestViewController): 您应该使用(在TestViewController中)设置委托:

self.delegate = self;

This is assuming that you want TestViewController to be the delegate, it wasn't clear to me if that's what you wanted. 这是假设您想让TestViewController成为委托,但我不清楚这是否是您想要的。

Have you tried linking them in Interface Builder? 您是否尝试过在Interface Builder中链接它们? Add this to your header file first though @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 首先通过@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;将其添加到头文件中@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM