TabBarController中NavController下ViewController的初始化
我有一个相对常见的 TabBarController 设置,其选项卡包含以 TableViewControllers 作为根的导航控制器。我正在尝试对这些 TableViewController 之一的初始化执行一些逻辑,但似乎无法找到调用的 init 函数。
我的目标是在 TableViewController (我已对其进行子类化)中添加一个侦听器,该侦听器可以通过更新 navigationController.tabBarItem.badgeVluew 属性来响应事件。
我尝试将代码放入 initWithStyle: 以及 init 中,但最终都没有被调用。我也尝试将其放入 viewDidLoad 中,但只有在控制器实际出现时才会被调用(我需要在控制器加载后/标签栏项目显示后立即发生)。
有谁知道我应该把这段代码放在哪里,以便在控制器初始化时发生?
此外,这都是通过界面构建器/NIB 设置的。我没有手动添加导航控制器或 tableviewcontroller,这就是为什么不清楚我需要重写哪个 init 函数。
I have the relatively common setup of a TabBarController whose tabs contain NavigationControllers which have TableViewControllers as their roots. I'm trying to perform some logic on initialization of one of these TableViewControllers but can't seem to find what init function gets called.
My goal is to add a listener in the TableViewController (that I have subclassed) which can respond to events by updating the navigationController.tabBarItem.badgeVluew property.
I've tried putting code into initWithStyle: as well as init but neither of them end up getting called. I've also tried putting it in viewDidLoad, but that only gets called once the controller actually appears (I need to have it happen as soon as the controller is loaded / as soon as the tab bar item shows up).
Does anyone know where I would put this code for it to happen on initialization of the controller?
Also, this is all set up through interface builder / NIBs. I'm not adding the nav controller or tableviewcontroller manually, which is why it's not clear what init function I need to override.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 IB 中选择 UITabBarItems 之一,您将看到“从“YourView”加载的视图”。单击进入此“灰色”视图。在检查器窗口中,您将在“属性”选项卡(左侧的选项卡)中看到标题和将加载的 NIB 名称(我们将其称为“YourNibName”)。
现在选择检查器的右侧选项卡(身份)并将类名(类旁边的组合)更改为您必须在 xcode 中创建的“YourViewController”类。不要使用已选择的标准 ViewController。 InterfaceBuilder 加载您的 nib 并将其附加到您的 ViewController。
打开 YourNibName 并将 FilesOwner 的类(检查器,右侧选项卡)也更改为“YourViewController”。
TabBar 的 NIB 也包含一个 FilesOwner。为此 FilesOwner 创建一个 ViewController 并将其类设置为此控制器(即 TabBarController)
在“TabBarController”中,您可以使用以下代码找出选择了哪个选项卡:
在这里您可以执行“全局”操作或预初始化某些操作。这是你能做的一件事。
初始化您的视图:
如果您选择一个选项卡并且视图(由 YourViewController 处理)将首次显示,“viewDidLoad”将在“YourViewController”中调用,
我希望这就是您的问题。
现在介绍一下徽章。这是一个 hack,但对我来说效果很好。
头文件:
向控制器添加一个插座,它代表您的 TabBarController:
将 IB 中的此插座与您的 TabBar 连接。
实现:
在 TabBarControllerClass 中,您可以覆盖“initWithNibName”:
如果您不使用 nib,请改用“- (void)loadView { ... }”。
您正在使用 TabBar 控制器的子类,也许您可以使用 'self.selectedIndex = 1;'而不是“tabBarController.selectedIndex = 1;”,等等。试试这个
希望这有帮助!
If you select one of your UITabBarItems in IB, you will see 'View loaded from "YourView"'. Click into this "gray" View. In the Inspector window you will see in the Attributes Tab (the tab on the left) the title and the NIB name which will be loaded (lets call it "YourNibName").
Now select the right tab of the inspector (Identity) and change the Classname (Combo next to Class) to your "YourViewController" class, which you must create in xcode. Don't use the standard ViewController, which is already selected. The InterfaceBuilder loads your nib and attaches it to your ViewController.
Open YourNibName and change FilesOwner's Class (Inspector, right Tab) to "YourViewController", too.
Your TabBar's NIB contains a FilesOwner, too. Create a ViewController for this FilesOwner and set its Class to this Controller (i.e. TabBarController)
In "TabBarController" you can find out which Tab was selected by using this code:
Here you can do something "global" or preinitialize something. This is ONE thing you can do.
INIT OF YOUR VIEWS:
If you select a Tab and the view (which is handled by YourViewController) will be shown for the first time, "viewDidLoad" will be called in "YourViewController"
I hope this is what your question was about.
Now something about the badge. It's a hack, but works fine for me.
Header file:
Add an outlet to your controller, which is representing your TabBarController:
Connect this outlet in IB with your TabBar.
Implementation:
In your TabBarControllerClass you can overwrite 'initWithNibName':
if you don't use nib, use '- (void)loadView { ... }' instead.
You are using a subclass of the TabBar controller, maybe you can use 'self.selectedIndex = 1;' instead of 'tabBarController.selectedIndex = 1;', and so on. Just try this out
Hope this helps!