【问题标题】:How do I make my annotation callout lead to a new View Controller?如何使我的注释标注导致新的视图控制器?
【发布时间】:2014-08-16 22:43:16
【问题描述】:

到目前为止,我的情节提要中有一个 DetailViewController ,没有转接/转接。这是我在地图视图控制器中的代码...

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"User"];


UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = rightButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
UIImageView *myCustomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CorgiRunner2 Final.png"]];
myCustomImage.image = profPic;
myCustomImage.frame = CGRectMake(0,0,31,31);
MyPin.leftCalloutAccessoryView = myCustomImage;

[rightButton addTarget:self action:@selector(pinTouched:) forControlEvents:UIControlEventTouchUpInside];

return MyPin;
}




-(void)pinTouched:(UIButton *)sender
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

detailViewController.profName1 = profName;
detailViewController.profPic1 = profPic;
}

我怎样才能在按下调用时显示我的详细视图控制器?

【问题讨论】:

  • 请注意:到目前为止,您的所有问题都已被 SO 上的其他用户多次询问和回答。在发布可能重复的问题之前,请在 SO、文档和其他地方彻底搜索(并且再次重新发布您自己的问题也不是一个好方法——改为编辑您现有的问题并解释为什么发布的答案对您不起作用)。
  • 对于您当前的问题,请参阅stackoverflow.com/questions/14805954/… 以获取示例。
  • 顺便说一句,如果你要使用内置委托方法 calloutAccessoryControlTapped,请不要使用 addTarget。如果您设置了附件视图并且设置了它的委托,则地图视图将自动调用其委托方法。

标签: ios objective-c annotations callout


【解决方案1】:

像这样使用calloutAccessoryControlTapped

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"tapped");
    DetailViewController *detailViewController = [[DetailViewController alloc]     initWithNibName:@"DetailViewController" bundle:nil];

    detailViewController.profName1 = profName;
    detailViewController.profPic1 = profPic;
}

不要忘记设置您的 mapView 委托并确保调用此委托方法。

希望对你有帮助

【讨论】:

  • 官方如何设置mapView代表?
  • 另外,您提供的方法仍然没有改变视图。
  • 如果你不设置委托,这个方法将不会被调用。
  • @Ty_,我认为 OP 的观点是这段代码实际上并没有显示 detailViewController,因为显示的所有代码都是创建 VC,但实际上并没有使用 segue 显示它( OP 正在使用情节提要)。
  • 在问题中,它说他正在使用情节提要,并且当前问题的代码也没有显示 VC。它只是创建它。
【解决方案2】:

更新 我改为将我的 mapView 连接到主故事板中的 DetailView 控制器,并使用了

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"Details" sender:view];
}

这对我有用

【讨论】:

    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    相关资源
    最近更新 更多