2012-08-26 57 views
1

我正在嘗試在Objective-C iPhone應用程序的Storyboard中的兩個ViewControllers之間傳遞對象。 (iOS 5/XCode 4)在視圖控制器與故事板之間傳遞對象(XCode 4/iOS 5)

第一個ViewController是帶有註釋的映射(每個註解對象稱爲MyLocation,它通過包含MyLocationView的地圖上顯示)。用戶單擊註釋,出現標註,然後用戶單擊右箭頭加載標註詳細信息視圖(新的ViewController),該視圖顯示所選標註的更多詳細信息。

我定義了一個segue'SegueAnnotationDetail',它在主視圖控制器和註解詳細視圖控制器之間移動。

我已經定義在主視圖控制器的一個實例變量來保存用戶點擊的註釋,然後我將它傳遞給註釋詳細視圖(ViewController.h):

@interface ViewController : UIViewController <MKMapViewDelegate> { 
    MKAnnotationView *selectedAnnotation; 
} 

因此,在主視圖控制器(ViewController.m)我有以下:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control 
{ 
    selectedAnnotation = view; 

    [self performSegueWithIdentifier:@"SegueAnnotationDetail" sender:self]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"SegueAnnotationDetail"]) 
    { 
     LocationDetailViewController *vc = [segue destinationViewController]; 

     [vc setLocation:selectedAnnotation]; 
    } 
} 

然後在LocationDetailViewController(其爲第二視圖控制器顯示每個註釋的細節),我

@interface LocationDetailViewController : UIViewController 

@property MKAnnotationView* location; 

@end 

每個註釋是MKPinAnnotationView並且創建如下,在viewForAnnotation方法,還內ViewController.m

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 

    static NSString *identifier = @"MyLocation"; 

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    // removed some code related to animation view queue here for simplicity 

    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 

} 

的問題是,當該運行時,該應用只是崩潰在該行:

[vc setLocation:selectedAnnotation]; 

UPDATE

在控制檯的錯誤信息是:

2012-08-26 18:33:10.615 ArrestsPlotter[79918:c07] -[UIViewController setLocation:]: unrecognized selector sent to instance 0x6ea0b60 
2012-08-26 18:33:10.615 ArrestsPlotter[79918:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setLocation:]: unrecognized selector sent to instance 0x6ea0b60' 
*** First throw call stack: 
(0x165e022 0x12cecd6 0x165fcbd 0x15c4ed0 0x15c4cb2 0x3977 0x85c4be 0x4f95ab 0x385b 0x361f8c 0x36d8ba 0x165fe99 0x43414e 0x4340e6 0x4daade 0x4dafa7 0x4da266 0x4593c0 0x4595e6 0x43fdc4 0x433634 0x1da5ef5 0x1632195 0x1596ff2 0x15958da 0x1594d84 0x1594c9b 0x1da47d8 0x1da488a 0x431626 0x231d 0x2285) 

終止叫做拋出異常(LLDB)

當我註釋掉這些行,應用程序運行正常儘管沒有顯示詳細信息頁面右側的信息。

WTF我做錯了嗎?

+0

當它崩潰時會出現什麼錯誤? (如果你沒有得到,可以嘗試將壞行放入@ try/@ catch並記錄例外信息,包括堆棧符號。) –

+0

如果發佈崩潰日誌,您可能會收到更多有用的回覆。這可能與故事板無關 - 它可能是你的註釋實際上不是註釋對象等等。你的崩潰日誌將有助於協助。 – lxt

+0

感謝您的回覆 - 我已經張貼錯誤消息 – Imme22009

回答

2

好的,我自己回答了。我會在這裏發佈答案,因爲它在互聯網上並不那麼清楚。

要將視圖控制器文件與故事板中的指定場景相關聯,請單擊場景,然後使用屬性檢查器,選擇自定義類並選擇要使用的類。

這就是你如何鏈接視圖控制器與場景。那是什麼導致了這裏的問題(請參閱第一個答案的評論)

相關問題