我尝试使用calloutAccessoryControlTapped
方法将注释标题传递给第二个视图控制器,如下所示:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
//My second VC's UILabel outlet
self.lblAnnotationTitle.text = view.annotation.title;
[self performSegueWithIdentifier:@"gymDetails" sender:self];
}
当视图控制器出现时,标签尚未更新。为什么会这样?
此外,这是将属性传递给另一个视图控制器的正确方法吗?如果没有,有什么更好的方法呢?
答案 0 :(得分:1)
更好的方法是在-prepareForSegue
方法内部进行
在该方法中,通过查看标识符,您可以获取destinationViewController
并使用您的模型进行更新
注意那些方面:
-viewDidLoad
方法内的标签。答案 1 :(得分:1)
您应该实现prepareForSegue
并将值传递给
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"gymDetails"]) {
GymDetailViewController *destViewController = segue.destinationViewController;
destViewController.ivar = @"Your Text here";
}
}
然后在你的视图中确实加载了你应该做的事情
lblAnnotationTitle.text = ivar;
答案 2 :(得分:0)
试试这个:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "xxx" {
if let destination = segue.destination as? XXX {
destination.xx = xx
}
}
}