带有 UITabViewController 的 Xcode Storyboard - 从按钮更改选项卡

发布于 2024-12-28 03:15:26 字数 810 浏览 2 评论 0原文

我有一个使用 Storyboard 的项目设置,其中包含 UITabViewController 作为初始根视图。其中一个选项卡会加载一个 NavigationController,而该 NavigationController 又会加载一个自定义视图控制器类。

在自定义视图控制器中,我有一个导航栏按钮,我想触发一个操作,将根 UITabViewController 返回到它的第一个索引。我已经能够使用传统的 xib 结构来完成此操作,方法是将 appDelegate 类添加到 xib 并以这种方式将方法链接到按钮。

实际上,我希望按钮触发如下所示的代码:

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController=_tabBarController;

-(IBAction)handleHome:(id)sender{
    //How do I send a message to the tabBarController?
    [self.tabBarController setSelectedIndex:0];
}

是否可以使用 Storyboard 方法来做到这一点?我查看了 Segue,但这似乎不是我想要做的(从我所看到的情况来看,我无法与根 UITabViewController 进行对话)。

我已经使用 Responder 方法触发了 handeHome 方法,因此我实际上需要知道的是如何访问 Storyboard 中实例化的 tabViewController。

希望这个问题有意义,让我知道是否有什么我应该扩展的。

I've got a project setup using a Storyboard that contains a UITabViewController as the initial root view. One of the tabs loads a NavigationController that in turn loads a custom view controller class.

From the custom view controller, I have a navigation bar button that I want to trigger an action that returns the root UITabViewController to it's first index. I've been able to do this using a traditional xib structure by adding the appDelegate class to the xib and linking a method to the button that way.

Effectively, I want the button to trigger code that looks something like this:

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController=_tabBarController;

-(IBAction)handleHome:(id)sender{
    //How do I send a message to the tabBarController?
    [self.tabBarController setSelectedIndex:0];
}

Is it possible to do this with the Storyboard approach? I looked at Segue's but that doesn't seem to be what I'm trying to do (there is no way for me to talk to the root UITabViewController from what I can see).

I've got the handeHome method being triggered using the Responder approach, so really all I need to know is how to access the instantiated tabViewController in the Storyboard.

Hopefully this question makes sense, let me know if there is anything I should expand on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鸠书 2025-01-04 03:15:26

为什么不在自定义视图控制器中执行此操作?

- (IBAction)handleHome:(id)sender {
    self.tabBarController.selectedIndex = 0;
}

tabBarController 属性内置于 UIViewController 中。

Why not just do this in your custom view controller?

- (IBAction)handleHome:(id)sender {
    self.tabBarController.selectedIndex = 0;
}

The tabBarController property is built in to UIViewController.

叫思念不要吵 2025-01-04 03:15:26

我想通了。我将引用的代码块更新为:

@implementation AppDelegate

@synthesize window = _window;

-(IBAction)handleHome:(id)sender{

    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;

    [tabViewController setSelectedIndex:0];

}

叹气...在询问有关 SO 的问题之前需要更多咖啡

I figured it out. I updated the quoted block of code to this:

@implementation AppDelegate

@synthesize window = _window;

-(IBAction)handleHome:(id)sender{

    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;

    [tabViewController setSelectedIndex:0];

}

Sigh... need more coffee before asking questions on SO

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