How to add pin annotation on MKMap in split view ipad programming realtime
I have trouble with showing an annotation on MKMap in Detailpane in Split-View base
I have table in master pane(left)(popover) and when I selected a row in master pane, the detail pane (right) should have an annotation appear but it is not.
code in function didSelectRowAtIndexPath in SecondViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath anim开发者_高级运维ated:YES];
DetailViewController *dTVC = [[DetailViewController alloc] init];
Lat = 15.1025;
Long = 100.2510;
[dTVC setShowAnnotation];
}
Noted that Lat and Long are global variable
code in function setShowAnnotation in DetailViewController.m
- (void)setShowAnnotation {
[_mapView removeAnnotation:self.customAnnotation];
self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:Lat andLongitude:Long] autorelease];
[_mapView addAnnotation:self.customAnnotation];
}
when I selected row nothing happen I have to link function setShowAnnotion to a button and after selected row and nothing happen, have to pressed that button and the annotation will appear. So, what I missed?
Probably the main reason it's not working is that in didSelectRowAtIndexPath
, you are creating a new instance of DetailViewController
instead of using the already-displayed instance.
The new instance you create is also not being displayed (presented) so even though setShowAnnotation may be working, you can't see anything on the screen.
In the standard Split View-based app, the master pane on the left is called RootViewController (what you are calling SecondViewController). In the standard template, there is already a detailViewController
ivar which points to the currently-displayed instance of DetailViewController
.
You may want to start your app again from scratch using the standard template, make as few changes as possible and use the pre-coded functionality. Or, create a new project using the standard template and study how it works and modify your project accordingly.
Then, in didSelectRowAtIndexPath
, instead of creating a new instance of DetailViewController
, just call setShowAnnotation
on the ivar:
[detailViewController setShowAnnotation];
Another thing I would suggest is instead of using global variables to "pass" the coordinates to the detail view, add the latitude and longitude as parameters to the setShowAnnotation
method itself.
So the method declaration would be something like:
- (void)setShowAnnotationWithLat:(CLLocationDegrees)latitude
andLong:(CLLocationDegrees)longitude;
and it would be called like this:
[detailViewController setShowAnnotationWithLat:15.1025 andLong:100.251];
and get rid of the global variables.
精彩评论