MapView:使用MKUserTrackingBarButtonItem切换地图跟踪状态
我已经添加了一个MKUserTrackingBarButtonItem到我的工具栏。 但是,点击它什么都不做。 所以,我认为我需要像使用普通的UIBarButtonItem一样使用addTarget,但是这也不起作用。
我如何将一个方法附加到该button,以便我可以更改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];
MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView]; [trackButton setTarget:self]; [trackButton setAction:@selector(track:)]; [toolbar setItems:[NSArray arrayWithObjects:trackButton, nil] animated:YES];
我想你应该用这个方法初始化MKUserTrackingBarButtonItem:
– initWithMapView:
请参阅文档: http : //developer.apple.com/library/ios/#documentation/MapKit/Reference/MKUserTrackingBarButtonItemClassRef/Reference/Reference.html