2012-08-26 66 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

好的,我自己回答了。我会在这里发布答案,因为它在互联网上并不那么清楚。

要将视图控制器文件与故事板中的指定场景相关联,请单击场景,然后使用属性检查器,选择自定义类并选择要使用的类。

这就是你如何链接视图控制器与场景。那是什么导致了这里的问题(请参阅第一个答案的评论)

相关问题