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

时间:2011-11-14 02:57:09

标签: iphone objective-c ios mkmapview mapkit

我已将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];

2 个答案:

答案 0 :(得分:3)

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

– initWithMapView:

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

答案 1 :(得分:2)

MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];   
[trackButton setTarget:self];
[trackButton setAction:@selector(track:)];

[toolbar setItems:[NSArray arrayWithObjects:trackButton, nil] animated:YES];