gpt4 book ai didi

iphone - 使用 Storyboard在 View Controller 之间传递对象 (XCode 4/iOS 5)

转载 作者:行者123 更新时间:2023-11-29 11:09:36 25 4
gpt4 key购买 nike

我试图在 Objective-C iPhone 应用程序的 Storyboard中的两个 ViewController 之间传递一个对象。 (iOS 5/XCode 4)

第一个ViewController是一个带注解的 map (每个注解对象叫做MyLocation,它通过一个包含MyLocationView的 View 显示在 map 上)。用户单击注释,出现标注,然后用户单击向右箭头以加载注释详细信息 View (新的 ViewController),该 View 显示有关所选注释的更多详细信息。

我已经定义了一个 segue 'SegueAnnotationDetail',它在主视图 Controller 和注释细节 View Controller 之间移动。

我在主视图 Controller 中定义了一个实例变量来保存用户点击的注解,然后我将把它传递给注解详情 View (ViewController.h):

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

所以在主视图 Controller (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(这是显示每个注释的详细信息的第二个 View Controller )中,我有

@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];

更新

控制台中的错误信息是:

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 我做错了吗?

最佳答案

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

要将 View Controller 文件与 Storyboard中的指定场景相关联,请单击该场景,然后使用属性检查器,选择自定义类并选择要使用的类。

这就是将 View Controller 与场景链接的方式。这就是导致这里出现问题的原因(请参阅对第一个答案的评论)

关于iphone - 使用 Storyboard在 View Controller 之间传递对象 (XCode 4/iOS 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com