0

我想使用 Storyboard 在 ViewControllers 之间传递信息,但出现错误...我的代码如下

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"ToAssistance"])
    {
        NSLog(@"To assistance");
    }
    else if([[segue identifier] isEqualToString:@"ToMap"])
    {
        UINavigationController * navController = (UINavigationController *)[segue destinationViewController];
        EventMapViewController * eventMapViewController = (EventMapViewController *)[navController topViewController];
    }
}

但在一线

EventMapViewController * eventMapViewController = (EventMapViewController *)[navController topViewController];

我得到以下异常

2012-04-23 19:49:00.369 StoryboardAssistance[9916:11603]
-[EventMapViewController topViewController]: unrecognized selector sent to instance 0x6e86380 2012-04-23 19:49:00.370 StoryboardAssistance[9916:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[EventMapViewController topViewController]: unrecognized selector sent to instance 0x6e86380'
*** First throw call stack: (0x135b022 0x2037cd6 0x135ccbd 0x12c1ed0 0x12c1cb2 0x546a 0x5424be 0x135ce99 0x11a14e 0x11a0e6 0x1c0ade 0x1c0fa7 0x1c0266 0x13f3c0 0x13f5e6 0x125dc4 0x119634 0x15fdef5 0x132f195 0x1293ff2 0x12928da 0x1291d84 0x1291c9b 0x15fc7d8 0x15fc88a 0x117626 0x218d 0x20f5) terminate called throwing an exception(lldb)

有什么建议么?

4

1 回答 1

2

According to your exception it seems that you are asking the "eventMapViewController" for its topviewontroller, which clearly doesnt exists since it is not a navigation controller. IF you check where you ask for it you can see here

[navController topViewController];

that navcontroller is in fact a controller of type "EventMapViewController", which would mean that this

UINavigationController * navController = (UINavigationController *)[segue destinationViewController];

is not giving you a uinavigationcontroller but in fact the "EventMapViewController" you want. so just remove those 2 lines and add

EventMapViewController * eventMapViewController = (EventMapViewController *)[segue destinationViewController];

if this doesnt work, then please comment this line

EventMapViewController * eventMapViewController = (EventMapViewController *)[navController topViewController];

and add a

NSLog(@"%@",navController);

and tell me the output.

于 2012-04-24T01:12:47.423 回答