通过选项卡栏访问 MKMapView

发布于 2024-08-19 00:17:37 字数 934 浏览 6 评论 0原文

我有一个选项卡栏应用程序,在第一个选项卡上有一个 MKMapView。我想要做的是从应用程序的其他位置将活动选项卡切换到地图视图,并根据前一个视图(带有切换到地图视图的按钮的视图)中的数据设置地图视图的区域。

我尝试过的是:

[self.tabBarController setSelectedView:0];
UIMapViewController *mapView = [self.tabBarController.viewControllers objectAtIndex:0];
[mapView displayBookmarkAnnotation:bookmark];

这只会导致应用程序崩溃,无法找到我创建的方法。 我不认为我已经选择了实现这一点的最佳路径,但我真的不确定我应该如何去做。

[更新] 转换 tabBarController 返回的控制器没有效果。

[已解决] 我试图将 UINavigationController 投射到我的地图视图

[self.tabBarController setSelectedView:0];
UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0];
//if the tab has other views open, return to mapView
[navController popToRootViewControllerAnimated:YES];
UIMapViewController *mapView = (UIMapViewController *)[navController visibleViewController];
[mapView customMessage:object];

I have a tabbar application and on the first tab I have a MKMapView. What I want to do is from somewhere else in the application, switch the active tab to the mapview and set the mapview's region based on the data in the previous view (the one with the button to switch to the mapview).

What I've tried is:

[self.tabBarController setSelectedView:0];
UIMapViewController *mapView = [self.tabBarController.viewControllers objectAtIndex:0];
[mapView displayBookmarkAnnotation:bookmark];

This just causes the app to crash unable to find the method I created.
I don't think I've chosen the best path to implement this but I'm really not sure how I should go about it.

[Update]
Casting the controller returned by the tabBarController had no effect.

[Solved]
I was trying to cast a UINavigationController to my mapView

[self.tabBarController setSelectedView:0];
UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0];
//if the tab has other views open, return to mapView
[navController popToRootViewControllerAnimated:YES];
UIMapViewController *mapView = (UIMapViewController *)[navController visibleViewController];
[mapView customMessage:object];

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

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

发布评论

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

评论(3

冷弦 2024-08-26 00:17:37

您确定该选项卡的主视图控制器不是 UINavigationController 吗?如果是这样,您可以获得 UIMapViewController 的根视图控制器。

如果您要从其他地方调用 AppDelegate,最好将直接引用放在 AppDelegate 中。

Are you sure the main view controller for that tab is not a UINavigationController? If so, you can get the root view controller for that which should be your UIMapViewController.

It would be good to put a direct reference in the AppDelegate though if you are going to be calling it from elsewhere.

夕色琉璃 2024-08-26 00:17:37

为什么不通过您的 AppDelegate 路由它? AppDelegate 可以有一个 UITabBarControllerMKMapView(两者都通过界面构建​​器连接)。然后,UIButton 处理程序也将位于 AppDelegate 中,以便它可以调用 -[UITabBarController setSelectedView:]-[MKMapView setRegion:]

Why not route it through your AppDelegate? The AppDelegate can have a UITabBarController and the MKMapView (both wired through interface builder.) The UIButton handler would then also be in the AppDelegate so that it can call -[UITabBarController setSelectedView:] and -[MKMapView setRegion:].

一抹微笑 2024-08-26 00:17:37

您想要做的是创建 UITabBarController 的子类或类别,

  1. 用于注册您定义的通知中心事件,并
  2. 使用新的选择器处理事件。我通常对它们使用 do/did 命名约定。

当事件发生时,您设置 selectedIndex。

What you want to do is create a subclass or a category of the UITabBarController that

  1. registers for NotificationCenter events that you define
  2. handles the events with a new selector. I generally use do/did naming convention for them.

When the event comes through you set the selectedIndex.

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