通过选项卡栏访问 MKMapView
我有一个选项卡栏应用程序,在第一个选项卡上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定该选项卡的主视图控制器不是 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.
为什么不通过您的 AppDelegate 路由它? AppDelegate 可以有一个
UITabBarController
和MKMapView
(两者都通过界面构建器连接)。然后,UIButton
处理程序也将位于 AppDelegate 中,以便它可以调用-[UITabBarController setSelectedView:]
和-[MKMapView setRegion:]
。Why not route it through your AppDelegate? The AppDelegate can have a
UITabBarController
and theMKMapView
(both wired through interface builder.) TheUIButton
handler would then also be in the AppDelegate so that it can call-[UITabBarController setSelectedView:]
and-[MKMapView setRegion:]
.您想要做的是创建 UITabBarController 的子类或类别,
当事件发生时,您设置 selectedIndex。
What you want to do is create a subclass or a category of the UITabBarController that
When the event comes through you set the selectedIndex.