检测从 TableViewController 内部在 TabBarController 上选择了哪个选项卡 - 代码重用
我有一个应用程序,它在 TabBarController 上有 5 个选项卡。为了简单起见,我们假设它们是选项卡 A、B、C、D 和 E。每个选项卡都会将用户带到嵌入导航控制器中的 TableViewController。每个选项卡还有其自己特定的 .h 和 .m 文件。 5 个选项卡之间的大部分代码非常相似。我想去掉这 5 组类文件,只使用 1 组。这将使我更容易对应用程序进行更改(在 1 个位置而不是 5 个位置)。如何在单个实施文件中检测选择了哪个选项卡?一旦我知道我可以放置逻辑来专门渲染选择了哪个选项卡的表格视图...
我应该提到的另一件事是我需要检测 TableViewController 中选定的选项卡。 TabBarController 是应用程序的入口点,我没有 TabBarController 子类。
我在 TableViewController 中尝试了这段代码,但是它没有被访问和/或使用。
在 .h 文件中:
@interface MyController : UITableViewController <UITabBarDelegate>
在 .m 文件中:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//NSLog(@"selectedIndex: %d", self.tabBarController.selectedIndex);
NSLog(@"didSelectItem: %d", item.tag);
}
I have an application which has 5 tabs on a TabBarController. For simplicity sake lets say they are Tab A, B, C, D, and E. Each tab takes the user to a TableViewController which is embedded in a Navigation controller. Each tab also has its own specific .h and .m files. The code for the most part is very similar between the 5 tabs. I want to do away with these 5 sets of class files and just use 1 set. This will make it much easier for me to make changes to the application (in 1 place instead of 5 places). How can I detect in the single implementation file which tab was selected? Once I know that I can put logic in place to render the tableview specifically for which tab was selected...
Another thing I should mention is that I need to detect the selected Tab in the TableViewController. The TabBarController is the point of entry for the application and I do not have a TabBarController subclass.
I tried this code in the TableViewController however it does not get accessed and/or used.
in .h file:
@interface MyController : UITableViewController <UITabBarDelegate>
in .m file:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//NSLog(@"selectedIndex: %d", self.tabBarController.selectedIndex);
NSLog(@"didSelectItem: %d", item.tag);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单,您已经有了解决方案!
这意味着您添加到选项卡栏控制器的任何 viewController 都具有由系统填充的此属性。
然后在您想要的选项卡视图控制器中实现 viewWillAppear
根据评论, tabBarController 的这个属性似乎并不可靠。
您描述的问题听起来像是可以通过子类化来解决的问题。为每个选项卡通用的代码创建 UIViewController 的子类,然后为每个选项卡 viewController 子类化您的子类,以进行选项卡特有的修改。
或者,您可以使用相同的类但不同的 xib 加载每个选项卡。您可以在界面生成器的“用户定义的运行时属性”部分中设置视图控制器的属性。然后在 viewWillAppear 块中检查 xib 在该实例上设置的属性。
Easy, You already have the solution!
That means that any viewController you add to a tab bar controller has this property filled in by the system.
Then in the view controller you want for the tab you implement viewWillAppear
In light of the comments this property of tabBarController doesn't seem to be reliable.
The problem you describe sounds like something that could solved by subclassing. Make a subclass of UIViewController for the code in common with each tab and then subclass your subclass for each tabs viewController to make modifications unique to the tab.
Alternatively you could load each tab with the same class but a different xib. You can set properties on your view controller in the "user defined runtime attributes" section in interface builder. Then in the viewWillAppear block just check the property set by the xib on that instance.
如果我理解正确的话,你有很多选择:
- 您可能想要重写 m 文件中的
init
方法,我猜它会初始化UITableViewCOntroller
并根据您所在的选项卡向其传递一个附加参数。您可能还想向此类添加一个
tabid
属性,并在为每个选项卡创建它时进行设置(显示您所在的选项卡)。您也可以使用通知(但这不会是最简单或最好的解决方案,除非您有充分的理由不使用前两个)
我确信还有很多其他方法。
If I understood you correctly, you have many choices:
- you may want to override the
init
method in your m file which I guess initializes aUITableViewCOntroller
and pass an additional parameter to it depending on which tab you are in.you may also want to add a
tabid
property to this class and set that when you are creating it for each tab (to something that shows which tab you are in).you mat also use notifications (but it wont be the easiest or best solution, unless you have good reason not to use the first two)
I am sure there are lots of other ways.