检测从 TableViewController 内部在 TabBarController 上选择了哪个选项卡 - 代码重用

发布于 2024-12-28 11:38:40 字数 784 浏览 1 评论 0原文

我有一个应用程序,它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2025-01-04 11:38:40

很简单,您已经有了解决方案!

tabBarController

作为选项卡栏控制器的父级或祖先。
(只读)

@property(非原子,只读,保留)UITabBarController *tabBarController

讨论如果接收器添加到标签栏控制器,则该属性是标签栏控制器。如果接收者的
导航控制器被添加到标签栏控制器,这个属性
是导航控制器的标签栏控制器。如果没有标签栏
存在或接收者是模态视图,此属性为零。

这意味着您添加到选项卡栏控制器的任何 viewController 都具有由系统填充的此属性。

然后在您想要的选项卡视图控制器中实现 viewWillAppear

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUInteger selectedIndex = self.tabBarController.selectedIndex;
    switch (selectedIndex) {
        case 0:
            //configure me
            break;
        case 1:
            //configure me differently!!
            break;

        default:
            break;
    }
}    

根据评论, tabBarController 的这个属性似乎并不可靠。

您描述的问题听起来像是可以通过子类化来解决的问题。为每个选项卡通用的代码创建 UIViewController 的子类,然后为每个选项卡 viewController 子类化您的子类,以进行选项卡特有的修改。

或者,您可以使用相同的类但不同的 xib 加载每个选项卡。您可以在界面生成器的“用户定义的运行时属性”部分中设置视图控制器的属性。然后在 viewWillAppear 块中检查 xib 在该实例上设置的属性。

Easy, You already have the solution!

tabBarController

A parent or ancestor that is a tab bar controller.
(read-only)

@property(nonatomic, readonly, retain) UITabBarController *tabBarController

Discussion If the receiver is added to a tab bar controller, this property is the tab bar controller. If the receiver’s
navigation controller is added to a tab bar controller, this property
is the navigation controller’s tab bar controller. If no tab bar is
present or the receiver is a modal view, this property is nil.

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

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUInteger selectedIndex = self.tabBarController.selectedIndex;
    switch (selectedIndex) {
        case 0:
            //configure me
            break;
        case 1:
            //configure me differently!!
            break;

        default:
            break;
    }
}    

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.

帥小哥 2025-01-04 11:38:40

如果我理解正确的话,你有很多选择:
- 您可能想要重写 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 a UITableViewCOntroller 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文