【问题标题】:prepare for segue is not letting me set NSString of next viewController准备 segue 不让我设置下一个 viewController 的 NSString
【发布时间】:2014-12-18 23:25:30
【问题描述】:

我想设置一个从 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) 

【问题讨论】:

  • 什么是崩溃信息?
  • 给出崩溃错误信息。

标签: ios objective-c storyboard segue


【解决方案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;
    }
}

【讨论】:

  • @Mike:你在那里得到了一个导航控制器,所以你需要使用 UINavigationController *navController = [segue destinationViewController]; ViewControllerSubCat *subCatVC = (ViewControllerSubCat *)navController.topViewController;
  • 它工作! :) 你能解释一下原因吗?
  • @Mike:我在自己的答案中添加了它。在您的设计中,您的 segue 连接到 UINavigationController,UINavigationController 连接到 ViewControllerSubCat。所以当你调用destinationViewController 时。您将获得 UINavigationController 实例而不是 ViewControllerSubCat。因此,您需要调用 UINavigationController 上的 topViewController,以获取您的视图控制器对象。
【解决方案2】:

正如其他发帖人所说,目标视图控制器不是您的自定义类。听起来你已经解决了。

您可能会遇到的另一个问题:

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

这一行:

subCatVC.navigationItem.title = subCatVC.cellNameCat;

可能不会工作,因为我怀疑你的新 VCs navigationItem 是否已经加载。

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

【讨论】:

  • 我不确定你在说什么,但如果我是正确的,我想你是想说当我将一个项目从 viewController A 传递到 viewController B 时,我通过它到一个导航栏的标题,我认为你说它不起作用?
  • 正确。这就是我要说的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多