Reputation: 42173
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];
Upvotes: 1
Views: 4346
Reputation: 36
MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];
[trackButton setTarget:self];
[trackButton setAction:@selector(track:)];
[toolbar setItems:[NSArray arrayWithObjects:trackButton, nil] animated:YES];
Upvotes: 2
Reputation: 593
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
Upvotes: 3