我查看了与viewcontrollers之间传递数据相关的所有其他帖子,但没有一个是针对我的问题。
我想将有人在mapKit中点击的注释标题传递给新的viewcontroller,以便我可以在新的viewcontroller中使用该标题。我正在使用calloutAccessoryControlTapped
方法检测某人是否点击了detailDisclosure按钮,然后使用以下代码将标题添加到第二个viewcontroller中的属性(名为detailViewController):
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
detailViewController *viewController = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailViewController *detailView = [[detailViewController alloc] init];
// THIS IS WHERE I SET THE ANNOTATION TITLE TO THE PROPERTY IN THE SECOND VIEWCONTROLLER
detailView.titleTime = view.annotation.title;
NSLog(@"view.annotation.title: %@", view.annotation.title); // SHOWS THE TITLE
NSLog(@"detailView.titleTime: %@", detailView.titleTime); // SHOWS THE TITLE
// HERE I PRESENT NEW VIEWCONTROLLER (HOPING THE TITLE IS PASSED)
[self presentViewController:viewController animated:YES completion:nil];
}
在第二个viewcontroller(detailViewController)中,我在viewDidLoad
方法中有这个:
- (void)viewDidLoad
{
[super viewDidLoad];
// set the Title Label to the titleTime (passed from the mainViewController)
self.titleForDetail.text = self.titleTime;
NSLog(@"titleForDetail.text %@:", titleForDetail.text); // THIS IS NULL
NSLog(@"titleForDetail.text %@:", titleTime); // THIS IS NULL
}
为什么self.titleForDetail.text
和self.titleTime NULL
?为什么没有从原来的viewController传递任何内容?
答案 0 :(得分:1)
在calloutAccessoryControlTapped
中,代码在titleTime
上设置detailView
属性,但随后展示viewController
(没有设置属性)。
将礼物更改为:
[self presentViewController:detailView animated:YES completion:nil];
看起来您不需要storyboard
和viewController
变量,因此您可以从该方法中删除它们。
答案 1 :(得分:1)
您的代码中包含以下两行:
detailViewController *viewController = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailViewController *detailView = [[detailViewController alloc] init];
在其余代码中,您有时使用viewController,有时使用detailView。你应该只有其中一个,它应该像你对viewController一样实例化:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
detailViewController *detailView = (detailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"detailViewController"];
detailView.titleTime = view.annotation.title;
[self presentViewController:detailView animated:YES completion:nil];
}