MapView:使用 MKUserTrackingBarButtonItem 切换地图跟踪状态

发布于 2024-12-15 14:32:52 字数 1549 浏览 0 评论 0原文

我已将 MKUserTrackingBarButtonItem 添加到我的工具栏。但是,单击它没有任何作用。所以,我假设我需要像普通的 UIBarButtonItem 一样使用 addTarget ,但这也不起作用。

如何将方法附加到该按钮以便更改 setUserTrackingMode:animated:

MKUserTrackingBarButtonItem *trackingItem = [[[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView] autorelease];
    UISegmentedControl *segmentedControl = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"List", @"Detail", nil]] autorelease];
    segmentedControl.frame = CGRectMake(0, 0, 220, 30);
    [segmentedControl setWidth:100.0 forSegmentAtIndex:0];
    [segmentedControl setWidth:100.0 forSegmentAtIndex:1];
    [segmentedControl setSelectedSegmentIndex:0];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *segmentedButton = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
    UIBarButtonItem *pageCurl = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:nil] autorelease];
    pageCurl.tintColor = [UIColor lightGrayColor];
    UIBarButtonItem *leftFlex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    UIBarButtonItem *rightFlex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    self.toolbar.items = [NSArray arrayWithObjects:trackingItem, leftFlex, segmentedButton, rightFlex, pageCurl, nil];

I have added a MKUserTrackingBarButtonItem to my toolbar. But, clicking it does nothing. So, I assumed I need to use addTarget like a normal UIBarButtonItem but that doesn't work either.

How can I attach a method to that button so that I can change setUserTrackingMode:animated:?

MKUserTrackingBarButtonItem *trackingItem = [[[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView] autorelease];
    UISegmentedControl *segmentedControl = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"List", @"Detail", nil]] autorelease];
    segmentedControl.frame = CGRectMake(0, 0, 220, 30);
    [segmentedControl setWidth:100.0 forSegmentAtIndex:0];
    [segmentedControl setWidth:100.0 forSegmentAtIndex:1];
    [segmentedControl setSelectedSegmentIndex:0];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *segmentedButton = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
    UIBarButtonItem *pageCurl = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:nil] autorelease];
    pageCurl.tintColor = [UIColor lightGrayColor];
    UIBarButtonItem *leftFlex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    UIBarButtonItem *rightFlex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    self.toolbar.items = [NSArray arrayWithObjects:trackingItem, leftFlex, segmentedButton, rightFlex, pageCurl, nil];

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

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

发布评论

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

评论(2

浅听莫相离 2024-12-22 14:32:52

我认为您应该使用此方法初始化 MKUserTrackingBarButtonItem:

– initWithMapView:

请参阅文档: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKUserTrackingBarButtonItemClassRef/Reference/Reference.html

I think you should initialize the MKUserTrackingBarButtonItem with this method:

– initWithMapView:

Refer to the documentation: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKUserTrackingBarButtonItemClassRef/Reference/Reference.html

云淡风轻 2024-12-22 14:32:52
MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];   
[trackButton setTarget:self];
[trackButton setAction:@selector(track:)];

[toolbar setItems:[NSArray arrayWithObjects:trackButton, nil] animated:YES];  
MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];   
[trackButton setTarget:self];
[trackButton setAction:@selector(track:)];

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