将字符串值从地图视图传递到详细信息视图

发布于 2024-12-10 03:44:00 字数 1191 浏览 2 评论 0原文

我试图在单击标注按钮后将字符串变量从地图视图控制器传递到详细信息视图控制器。我获取了标注按钮的标题并将其存储在 NSString 对象中(该对象已在地图视图控制器中合成)。

然后,我在详细视图控制器中分配并初始化了地图视图控制器,并尝试在新视图控制器中记录该值一次,结果返回为 null。代码如下:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    TheLocations *annotationTapped = (TheLocations *)view.annotation;

    NSLog(@"button clicked on annotaion %@", annotationTapped);

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    dvc.title = view.annotation.title;
    pinTitle = dvc.title;
    NSLog(@"%@", pinTitle);

    [thisMap switchViews:self.view toView:dvc.view];
}

这会成功记录“pinTitle”的值。 pinTitle 已创建并合成,因此它是此地图视图的字符串。

现在,我切换到“详细信息视图”,并将此代码输入到 viewDidLoad 方法中:

MapViewController *mvc = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    NSLog(@"%@", mvc.pinTitle);

它识别“pinTitle”并允许其注销,但返回“(null)”,因此在途中的某个地方 pinTitle 的值丢失了。当我切换视图时,如何让 pinTitle 保留其值?非常感谢您的帮助!

I am attempting to pass a string variable from a Map View Controller to a Detail View Controller after the click of a callout button. I took the title of the callout button and stored it in an NSString object (which had been synthesized in the Map View Controller).

I then allocated and initialized the Map View Controller in the Detail View Controller and attempted to log the value once in the new view controller and it returned as null. Here's the code:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    TheLocations *annotationTapped = (TheLocations *)view.annotation;

    NSLog(@"button clicked on annotaion %@", annotationTapped);

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    dvc.title = view.annotation.title;
    pinTitle = dvc.title;
    NSLog(@"%@", pinTitle);

    [thisMap switchViews:self.view toView:dvc.view];
}

This results in a successful log of "pinTitle"'s value. pinTitle has been created and synthesized, so it's a string of this Map View.

Now, I switch over to the Detail View and entered this code into the viewDidLoad method:

MapViewController *mvc = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    NSLog(@"%@", mvc.pinTitle);

It recognizes "pinTitle" and allows it to log out but returns "(null)" so somewhere along the way the value of pinTitle was lost. How do I get pinTitle to retain it's value when I switch views? Thanks so much for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-12-17 03:44:00

在详细视图的 viewDidLoad 方法中,您将创建一个 MapViewController 的新实例,该实例与 calloutAccessoryControlTapped 中引用的实例分开。

在这个新的单独实例中,pinTitle 未设置,默认值为 null。

我不确定为什么在任何情况下都需要 pinTitle 变量。

在 calloutAccessoryControlTapped 中,您正在设置 dvc.title ,以便在 DetailViewController 的 viewDidLoad 中访问该值使用self.title。您不需要引用MapViewController

In the detail view's viewDidLoad method, you are creating a new instance of MapViewController which is separate from the instance being referenced in calloutAccessoryControlTapped.

In that new, separate instance, pinTitle is not set and has a default value of null.

I'm not sure why you need the pinTitle variable in any case.

In calloutAccessoryControlTapped, you are setting dvc.title so in the viewDidLoad of the DetailViewController, you would access that value using self.title. You don't need to reference MapViewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文