向MKUserTrackingBarButtonItem添加一个Action

时间:2014-06-24 16:26:02

标签: ios ios7 mapkit

(使用Xcode 6)

我的跟踪按钮位于导航栏的右侧,它不会触发方法,我不知道为什么,这应该是正确的方式。

按钮本身功能正常。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //location initial settings:
    firstLaunch = YES;
    self.mapView.delegate = self;

    [self.mapView setShowsUserLocation:YES];

    locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    currentCentre = self.mapView.centerCoordinate;
    //create a thread queue
    [self queryGooglePlaces: currentCentre];

    //navBar:

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

    self.navigationItem.rightBarButtonItem = trackButton;
}

-(void) track: (id) sender {
    NSLog(@"clicked !");
}

1 个答案:

答案 0 :(得分:4)

我认为您不能以这种方式使用MKUserTrackingBarButtonItem - 它已经有一个目标(相关的地图视图)和一个动作(更改跟踪模式)。相反,当单击按钮时,我会调查-[MKMapViewDelegate mapView:didChangeUserTrackingMode:animated:]以响应地图模式更改。