准备segue不让我设置下一个viewController的NSString

时间:2014-12-18 23:25:30

标签: ios objective-c storyboard segue

我想从viewController A到viewController B设置一个NSString。我尝试使用下面的代码,但它没有工作。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueSubCat"])
    {
        NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow];

        ViewControllerSubCat *subCatVC = [segue destinationViewController];
        subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row];
        subCatVC.navigationItem.title = subCatVC.cellNameCat;
    }
}

我试过了,每次进入第二个视图时应用程序都崩溃了。所以我用以下代码替换它:

wat = @"wat";
[[segue destinationViewController] setContentsName:wat];

它做了同样的事情。即它崩溃了。 我不确定我做错了什么。

注意:当我拿走prepareForSegue中的代码时,它就可以了。

错误消息

2014-12-18 15:36:16.382 myApp[1503:42438] -[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240
2014-12-18 15:36:16.388 myApp[1503:42438] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010094cf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001005e5bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010095404d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001008ac27c ___forwarding___ + 988
    4   CoreFoundation                      0x00000001008abe18 _CF_forwarding_prep_0 + 120
    5   myApp        0x00000001000b3887 -[ViewController prepareForSegue:sender:] + 343
    6   UIKit                               0x000000010128b71c -[UIStoryboardSegueTemplate _perform:] + 151
    7   UIKit                               0x0000000100e21360 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242
    8   UIKit                               0x0000000100e214d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    9   UIKit                               0x0000000100d5c331 _applyBlockToCFArrayCopiedToStack + 314
    10  UIKit                               0x0000000100d5c1ab _afterCACommitHandler + 516
    11  CoreFoundation                      0x0000000100881dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x0000000100881d20 __CFRunLoopDoObservers + 368
    13  CoreFoundation                      0x0000000100877b53 __CFRunLoopRun + 1123
    14  CoreFoundation                      0x0000000100877486 CFRunLoopRunSpecific + 470
    15  GraphicsServices                    0x0000000103f1b9f0 GSEventRunModal + 161
    16  UIKit                               0x0000000100d39420 UIApplicationMain + 1282
    17  myApp        0x00000001000b4523 main + 115
    18  libdyld.dylib                       0x0000000102edc145 start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:1)

首先检查您是否在故事板中将UIViewController课程指定为ViewControllerSubCat

修改

从崩溃日志中可以看出,UINavigationController导致您获得[segue destinationViewController];。所以改变代码如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueSubCat"])
    {
        NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow];
        UINavigationController *navController = [segue destinationViewController];
        ViewControllerSubCat *subCatVC = (ViewControllerSubCat *)navController.topViewController;
        subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row];
        subCatVC.navigationItem.title = subCatVC.cellNameCat;
    }
}

答案 1 :(得分:0)

正如其他海报所说,目标视图控制器不是您的自定义类。听起来你已经解决了这个问题。

您可能面临的另一个问题是:

在调用prepareForSegue时,目标VC的视图层次结构尚未加载。

这一行:

subCatVC.navigationItem.title = subCatVC.cellNameCat;

可能不会起作用,因为我怀疑你的新VC已经加载了navItem。

免责声明:我实际上从未试图在prepareForSegue方法中操纵目标视图控制器的navigationItem,所以我不确定它是否会被定义,但我强烈怀疑不会。要对此进行测试,请在prepareForSegue中记录subCatVC.navigationIte的值(一旦您获得指向正确VC的指针)