2013-04-22 160 views
0

我几乎遵循这个post。视图控制器将通过故事板推送。这里的相关代码:
ViewController.m:在ViewController之间传递地图数据

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"%@", view.annotation.title); 
    MapPoint *annView = view.annotation; 
    DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
    dvc.title = @"my own Details"; 
    dvc.titleTMP = annView.title; 
    dvc.subtitleTMP = annView.subtitle; 

    [self.navigationController pushViewController:dvc animated:YES]; 
} 

MapPoint.h:

@interface MapPoint : NSObject <MKAnnotation> 
{ 
    NSString *_title; 
    NSString *_subtitle; 
    CLLocationCoordinate2D _coordinate; 
    UIImage *_storeImage; 
} 

@property (copy) NSString *title; 
@property (copy) NSString *subtitle; 
@property (nonatomic, readonly) UIImage *storeImage; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 


- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage; 

和DetailViewController:

@interface DetailViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIImageView *branchImage; 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 
@property (strong, nonatomic) IBOutlet UITextView *textView; 

我觉得我的错误是在calloutAccessoryControlTapped方法但我不知道这个问题的真正原因是什么。

回答

0

我通过考虑加载顺序来解决它。
所有要做的就是创建临时@properties

@interface DetailViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIImageView *branchImage; 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 
@property (strong, nonatomic) IBOutlet UITextView *textView; 
@property (nonatomic) NSString *titleTMP;  //added 
@property (nonatomic) NSString *subtitleTMP;  //added 

,做

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"%@", view.annotation.title); 
    MapPoint *annView = view.annotation; 
    DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
    dvc.title = @"my own Details"; 
    dvc.titleTMP = annView.title; 
    dvc.subtitleTMP = annView.subtitle; 

    [self.navigationController pushViewController:dvc animated:YES]; 
} 

的DetailViewController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    titleLabel.text = titleTMP; 
    textView.text = subtitleTMP; 

} 
1

如果使用的是故事板,可以使用prepareForSegue方法和内设置和你所做的一样。

我发布的代码

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

if([segue.identifier isEqualToString:@"Brs"]){ 
    NSLog(@"segue %@ ",[segue identifier]); 
    [segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ]; 
    } 
} 

的一部分。在这个例子中我设置属性下的UIViewController“路径”只有当他的标识符是“BRS”。 使用此方法需要将UIviewController标识符设置为故事板右侧面板。 如果在故事板中有它,则不需要实例化新的UIViewController。