Iphone 如何在地图首次加载时自动打开地图上注释的标注?

Iphone 如何在地图首次加载时自动打开地图上注释的标注?,iphone,objective-c,annotations,mapkit,callouts,Iphone,Objective C,Annotations,Mapkit,Callouts,我正在为iPhone开发一个基于导航的应用程序,允许用户在地图上查看表格中的选择。我有一个注释,可以精确定位用户在地图上选择的位置。按照正常行为,如果用户单击注释,将显示一个标注,其中包含有关位置的详细信息。这里没问题 我的问题是,一旦用户被带到包含地图的屏幕上,我希望标注自动从注释中显示,这样用户就不必单击注释来查看有关位置的详细信息,但我不确定如何做到这一点。我的“MapViewController”类中有以下方法,其中执行了大部分地图显示工作: - (void)viewDidLoad {

我正在为iPhone开发一个基于导航的应用程序,允许用户在地图上查看表格中的选择。我有一个注释,可以精确定位用户在地图上选择的位置。按照正常行为,如果用户单击注释,将显示一个标注,其中包含有关位置的详细信息。这里没问题

我的问题是,一旦用户被带到包含地图的屏幕上,我希望标注自动从注释中显示,这样用户就不必单击注释来查看有关位置的详细信息,但我不确定如何做到这一点。我的“MapViewController”类中有以下方法,其中执行了大部分地图显示工作:

- (void)viewDidLoad {

   [super viewDidLoad];
MKCoordinateRegion region;
MKCoordinateSpan span;

NavButtonAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
userCoord = delegate.userLocation.coordinate;

region.center = userCoord; 
span.latitudeDelta = 0.4;
span.longitudeDelta = 0.4;
region.span = span;

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
[mapView setRegion:region animated:YES]; 

RestaurantAnnotation *rAnnotation = [[RestaurantAnnotation alloc] init];
rAnnotation.title = restaurantObj.name;  
rAnnotation.subtitle = restaurantObj.address;
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.hours]; 
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.phoneNumber]; 


CLLocationCoordinate2D newCoord = {restaurantObj.latitude, restaurantObj.longitude};
rAnnotation.coordinate = newCoord;
[mapView addAnnotation:rAnnotation];

}
在上一屏幕中,通过以下方法调用MapViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

Restaurant *rLocation = [restaurantList objectAtIndex:indexPath.row];

MapViewController *mapController = [[MapViewController alloc] initWithRestaurant:rLocation];
[self.navigationController pushViewController:mapController animated:YES];
[mapController release];
}
我意识到我必须使用以下方法来实现这一点:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:0];
    [mapView selectAnnotation:myAnnotation animated:YES];
}
-(void)mapView:(MKMapView*)mapView添加注释视图:(NSArray*)视图
{
id myAnnotation=[mapView.annotations对象索引:0];
[mapView selectAnnotation:myAnnotation动画:是];
}
然而,我不知道怎么做。我一次使用的注释不多,我只有一个需要使用的注释。

我应该将此方法放在我的应用程序中的何处,从何处调用它?
我是否从viewDidLoad方法调用此方法并将实际方法放入MapViewController类中?

您必须添加

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    id myAnnotation = [mapView.annotations objectAtIndex:0]; 
    [mapView selectAnnotation:myAnnotation animated:YES]; 
}

设置为MKMapView委托的类

非常感谢您的及时回复。然后我会从viewDidLoad方法调用该方法吗?不,您根本不必直接调用它。只要您设置了代理,地图视图就会自动调用它。非常感谢您的回答。你的解决方案奏效了!当心。