如何在 TabBarController 中的另一个选项卡中包含的 NavigationController 中推送视图?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要使用通知,解决方法是强制在显示第二个选项卡之前“创建”它。
像这样的东西:
If you're going to use notifications, the fix is to force the second tab to be "created" before it's displayed.
Something like:
我无权访问我的代码,但我做了类似的事情:
[[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.
我认为观察者/通知模式是正确的。但是,您通常希望“控制器”观察“模型”对象。
我将创建一个包含所选事件的模型对象。
当加载每个 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.