如何将数据发送到目标视图控制器?

时间:2015-03-09 16:39:33

标签: ios objective-c uiviewcontroller segue

我有一个segue,我想设置目标视图控制器ClueDetailViewController的自定义属性。我不知道这段代码有什么问题,但它在运行时崩溃,错误“无法识别的选择器发送到实例0x7fa802f9cb20”(目标视图控制器)。

ClueDetailViewController.h(目的地):

#import <UIKit/UIKit.h>

@interface ClueDetailViewController : UIViewController

@property NSString *testString;

@end

在TheHuntViewController.m中(触发segue的地方):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showClue"])
    {
        ClueDetailViewController *viewController = (ClueDetailViewController *)[segue destinationViewController];

        viewController.testString = @"This is a test"; // causes runtime crash
    }
}

请帮忙!谢谢。

编辑:以下是完整的错误消息:

2015-03-09 12:29:32.636 InternetOfDrinks1[93005:14003302] -[UINavigationController setTestString:]: unrecognized selector sent to instance 0x7fa802f9cb20
2015-03-09 12:29:32.638 InternetOfDrinks1[93005:14003302] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setTestString:]: unrecognized selector sent to instance 0x7fa802f9cb20'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104f8df35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104c26bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000104f9504d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000104eed27c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000104eece18 _CF_forwarding_prep_0 + 120
    5   InternetOfDrinks1                   0x000000010466d356 -[TheHuntTableViewController prepareForSegue:sender:] + 422
    6   UIKit                               0x00000001058cc71c -[UIStoryboardSegueTemplate _perform:] + 151
    7   InternetOfDrinks1                   0x000000010466d17e -[TheHuntTableViewController tableView:didSelectRowAtIndexPath:] + 142
    8   UIKit                               0x0000000105462393 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
    9   UIKit                               0x00000001054624d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    10  UIKit                               0x000000010539d331 _applyBlockToCFArrayCopiedToStack + 314
    11  UIKit                               0x000000010539d1ab _afterCACommitHandler + 516
    12  CoreFoundation                      0x0000000104ec2dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    13  CoreFoundation                      0x0000000104ec2d20 __CFRunLoopDoObservers + 368
    14  CoreFoundation                      0x0000000104eb8b53 __CFRunLoopRun + 1123
    15  CoreFoundation                      0x0000000104eb8486 CFRunLoopRunSpecific + 470
    16  GraphicsServices                    0x000000010855c9f0 GSEventRunModal + 161
    17  UIKit                               0x000000010537a420 UIApplicationMain + 1282
    18  InternetOfDrinks1                   0x000000010466e553 main + 115
    19  libdyld.dylib                       0x000000010751d145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:3)

看起来你把它添加到UINavigationViewController堆栈,试试这个:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showClue"])
    {
       UINavigationController *dest = (UINavigationController *)segue.destinationViewController;
        ClueDetailViewController *viewController = (ClueDetailViewController *)dest.topViewController;

        viewController.testString = @"This is a test"; // causes runtime crash
    }
}