2013-04-22 98 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。