refreshing mkannotation properties and refreshing the annotation


refreshing mkannotation properties and refreshing the annotation



I have a mkannotation located on a mapview which has a mkannotationview as well as a calloutview which when clicked goes to a child uiviewcontroller. I am updating some properties from the callout's uiviewcontroller, but after I'm finished I want to move the annotation's position on the map and change the annotation title and subtitle. How can I easily do this from the callout's uiviewcontoller? What's the most elegant way to handle this? A code sample would be great if anyone has any.

Thanks




Disable auto correction of iPhone text field

1:



UIActionSheet - Unknown Number Of Buttons, How to know what to call?
I'd create a protocol, say MapCallBackDelegate, to handle what you want to do.


JSCocoa and the iPhone
This avoids tightly coupled code.


ICMP and the iPhone SDK
Put this in your map annotation view header file.
How do I add a UIButton or UISwitch in tableView:viewForFooterInSection
@protocol MapCallBackDelegate     -(void)updateAnnotation:(id)whatEverParamsYouWant; @end 
Then make your Map View implement this protocol.


Generating cryptographically secure authentication tokens
When you create your map annotation view, give it a property.
What sort function is used in NSArray?
@property (nonatomic, retain) id<MapCallBackDelegate> callbackDelegate; 
And when you add it to your map, set that property to self.
Releasing a property (Objective-C)
myMapAnnotationView.callbackDelegate = self; 
so when you want to change the title/subtitle/position, you just invoke that message on the callbkacDelegate.

. This is elegant because it reduces tightly-coupled code, allows other objects to implement the same protocol for code reuse later, and promotes information hiding in your MapAnnotationView..


2:


Remove the annotation from the map entirely, update it, and add it to the map again.

That'll ensure that the map notices that the annotations location has changed.

.


3:


Although you can remove and add the annotation back as @Caleb suggests, another option is to update the coordinate property directly on the annotation you want to move.

. Note that this will only work if your annotation class implements setCoordinate which can easily be done by declaring the coordinate as assign (like the built-in MKPointAnnotation class does) instead of readonly.

The map view will see the change via KVO and move the annotation.. To have the child view controller tell the map view controller which annotation to change and what the new coordinates are, I recommend using delegate+protocol as another answer suggests..


4:


The easiest way would to actually not do it from the child view controller.

Maybe your needs are different from what I understand from the question, but at first blush I would do something like this:. In the header:.
@interface YourController {     ... 

MKAnnotation *_latestDetailViewed; } ...

@property(nonatomic, retain) MKAnnotation *latestDetailViewed; @end
Then in the .m something like.
@implementation YourController  ... 

@synthesize latestDetailViewed = _latestDetailViewed; ...

-(void) dealloc { ...

self.latestDetailViewed = nil; [super dealloc]; } -(void) whereverYouLaunchYourDetailScreenFrom:(MKAnnotation*)detailAnnotation { self.latestDetailViewed = detailAnnotation; // then create/push your view controller } -(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if(_latestDetailViewed) { // Do whatever you want to the annotation here self.latestDetailViewed = nil; } }
That way your changes will be made when you come back to the map.

If you're really only launching a detail view for one annotation at a time, and always coming back to the map in between, then it should work without making you deal with writing a delegate protocol or firing off NSNotifications.. If I'm misunderstanding your situation let me know and I'll give you a different answer :).



76 out of 100 based on 76 user ratings 526 reviews

*