Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add MKUserTrackingBarButtonItem in Interface Builder

Would anyone be able to show me how to add a MKUserTrackingBarButtonItem to my toolbar in Interface Builder? I have a UIBarButtonItem on my UIToolbar whose class I have set to MKUserTrackingBarButtonItem, but this doesn't seem the right way to do it.

I have the following property:

@property (nonatomic, strong) IBOutlet MKUserTrackingBarButtonItem *trackingButton;

And I can add the button in code by using:

trackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:toolbar.items];
[items insertObject:trackingButton atIndex:0];
[toolbar setItems:items];

But I'm just missing how to do it in IB.

like image 608
cachvico Avatar asked Nov 05 '11 22:11

cachvico


2 Answers

Unfortunately this doesn't seem possible in IB due to the designated initializer of MKUserTrackingBarButtonItem. You have to instantiate it and add it to the toolbar programmatically, as you are doing.

like image 53
Marco Avatar answered Oct 16 '22 22:10

Marco


You can just add a UIBarButtonItem, and then make it a MKUserTrackingBarButtonItem in the class field in the identity inspector/IB sidebar, and add the button as an IBOutlet and then for it to appear you have to set the mapView property programmatically. With swift this can be done nicely in didSet:

@IBOutlet weak var trackingButton: MKUserTrackingBarButtonItem! {
  didSet {
    trackingButton.mapView = self.mapView;
  }
}

Or you can subclass and make mapView an IBOutlet so you can connect it in IB:

class UserTrackingBarButtonItem : MKUserTrackingBarButtonItem {
  @IBOutlet override var mapView : MKMapView? {
    get {
      return super.mapView;
    }
    set {
      super.mapView = newValue;
    }
  }
}
like image 30
Jonathan. Avatar answered Oct 16 '22 23:10

Jonathan.