将字符串值从地图视图传递到详细信息视图
我试图在单击标注按钮后将字符串变量从地图视图控制器传递到详细信息视图控制器。我获取了标注按钮的标题并将其存储在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在详细视图的
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 ofMapViewController
which is separate from the instance being referenced incalloutAccessoryControlTapped
.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 settingdvc.title
so in theviewDidLoad
of theDetailViewController
, you would access that value usingself.title
. You don't need to referenceMapViewController
.