如何在 TabBarController 中的另一个选项卡中包含的 NavigationController 中推送视图?

发布于 2024-10-22 06:33:08 字数 1643 浏览 1 评论 0原文

我有一个带有 2 个选项卡的 TabBarController,其中一个是 MapView,另一个是 NavigationController 中的简单 TableView。两者都显示来自同一源的数据。如果点击表中的任何数据,我会将 DetailViewController 添加到 NavigationController 并显示更多详细信息。现在,在 MapView 上,当在地图中点击数据时,我还想打开此 DetailViewController。最好的方法是什么?我尝试了一些通知,但这效果不佳,因为发送通知后 TableViewController 已完成加载(并注册为观察者)。

这是我的代码:

MapViewController:

- (IBAction)goToNearestEvent:(id)sender {
    if (currentNearestEvent) {
        [[self tabBarController] setSelectedIndex:1];

        NSDictionary *noteInfo = [[NSDictionary alloc] initWithObjectsAndKeys:currentNearestEvent, @"event", nil];
        NSNotification *note = [NSNotification notificationWithName:@"loadDetailViewForEvent" object:self userInfo:noteInfo];
        [[NSNotificationCenter defaultCenter] postNotification:note];
        [noteInfo release];
    }
}

TableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self 
           selector:@selector(loadDetailViewForEvent:)
               name:@"loadDetailViewForEvent"
             object:nil];
}

- (void)loadDetailViewForEvent:(NSNotification *)note {
    Event *e = [[note userInfo] objectForKey:@"event"];
    [self loadEventDetailViewWithEvent:e];
}

所以我对 iOS / Cocoa 编程非常陌生。也许我的做法是错误的选择。所以我希望有人能告诉我如何以正确的方式解决这些问题。

我忘记清楚地声明我的结构:

- UITabBarController
  - MapView (1)
  - NavigationControllerContainer
    - NavigationControllerView (2)
       - TableView

我想将一个新视图从 MapView (1) 推送到 NavigationControllerView (2)。

I have a TabBarController with 2 tabs, in one is a MapView and in the other one a simple TableView in a NavigationController. Both display Data from the same source. If any Data in the table is tapped, I add a DetailViewController to the NavigationController and show more details. Now on the MapView I also want to open this DetailViewController when the Data is tapped in the map. What's the best way to do this? I tried some with Notification but this doesn't work well because the TableViewController is finished loading (and registered as an observer) after the Notification is sent.

Here's my code:

MapViewController:

- (IBAction)goToNearestEvent:(id)sender {
    if (currentNearestEvent) {
        [[self tabBarController] setSelectedIndex:1];

        NSDictionary *noteInfo = [[NSDictionary alloc] initWithObjectsAndKeys:currentNearestEvent, @"event", nil];
        NSNotification *note = [NSNotification notificationWithName:@"loadDetailViewForEvent" object:self userInfo:noteInfo];
        [[NSNotificationCenter defaultCenter] postNotification:note];
        [noteInfo release];
    }
}

TableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self 
           selector:@selector(loadDetailViewForEvent:)
               name:@"loadDetailViewForEvent"
             object:nil];
}

- (void)loadDetailViewForEvent:(NSNotification *)note {
    Event *e = [[note userInfo] objectForKey:@"event"];
    [self loadEventDetailViewWithEvent:e];
}

So I'm very new to iOS / Cocoa programming. Maybe my approach is the wrong choice. So I hope anybody could tell me how to solve such things the right way.

I forgot to declare my structure clearly:

- UITabBarController
  - MapView (1)
  - NavigationControllerContainer
    - NavigationControllerView (2)
       - TableView

I want to push a new View from the MapView (1) to the NavigationControllerView (2).

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

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

发布评论

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

评论(3

-残月青衣踏尘吟 2024-10-29 06:33:08

如果您要使用通知,解决方法是强制在显示第二个选项卡之前“创建”它。

像这样的东西:

UIViewController *otherController = [[[self tabBarController] viewControllers] objectAtIndex:1];
otherController.view; // this is magic;
// it causes Apple to load the view,
// run viewDidLoad etc,
// for the other controller
[[self tabBarController] setSelectedIndex:1];

If you're going to use notifications, the fix is to force the second tab to be "created" before it's displayed.

Something like:

UIViewController *otherController = [[[self tabBarController] viewControllers] objectAtIndex:1];
otherController.view; // this is magic;
// it causes Apple to load the view,
// run viewDidLoad etc,
// for the other controller
[[self tabBarController] setSelectedIndex:1];
撩发小公举 2024-10-29 06:33:08

我无权访问我的代码,但我做了类似的事情:

[[self.tabBarController.viewControllers objectAtIndex:1] PushViewController:detailViewAnimated:YES];

尝试一下并让我知道。

I don't have access to my code, but I did something similar to:

[[self.tabBarController.viewControllers objectAtIndex:1] pushViewController:detailView animated:YES];

Give this a try and let me know.

一梦等七年七年为一梦 2024-10-29 06:33:08

我认为观察者/通知模式是正确的。但是,您通常希望“控制器”观​​察“模型”对象。

我将创建一个包含所选事件的模型对象。

当加载每个 viewController 时,它会查看“Model”对象并将其自身定向到所选事件。

当任何视图控制器更改选定的事件时,它会在模型​​中执行此操作,然后通知传播到其他控制器。

I think the observer/notification pattern is the right one. However, you normally want "controllers" to observe "model" objects.

I would create a Model object that contains the selected Event.

When each viewController is loaded, it looks at the "Model" object and directs itself to the selected event.

When any of the viewControllers changes the selected event, it does so in the Model, and then the notification propagates to the other(s) controllers.

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