ios - 准备 segue 不允许我设置下一个 viewController 的 NSString

标签 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;
    }
}

我试过了,但每次我转到第二个 View 时应用程序都会崩溃。所以我用下面的代码替换了它:

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) 

最佳答案

首先检查您是否在 Storyboard中将 UIViewController 类指定为 ViewControllerSubCat

编辑:

从您的崩溃日志中可以清楚地看出,作为 [segue destinationViewController]; 的结果,您正在获得一个 UINavigationController。因此,将代码更改为:

- (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;
    }
}

关于ios - 准备 segue 不允许我设置下一个 viewController 的 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27557490/

相关文章:

ios - 是否可以为每个部分设置单元格背景颜色?

ios - 使用 NSLog 打印地址簿到控制台

swift - 在 iOS 13 上,导航栏的内容在模式中部分不可见

ios - 如何在 iOS 中设置字符限制

ios - <img> 在移动版 safari 上被裁掉了 1px

ios - 为什么窗口的 subview 最初定位不正确,但旋转后重新定位正确?

iphone - 动画 UITextView 时打字

ios - UITableView 单元格行为不当,显示没有披露等

iphone - 将 UIScrollView 手势识别器添加到另一个 UIScrollView

ios - 如何在动画对象上启用用户交互?