performSegueWithIdentifier vs instantiateViewControllerWithIdentifier [英] performSegueWithIdentifier vs instantiateViewControllerWithIdentifier

查看:109
本文介绍了performSegueWithIdentifier vs instantiateViewControllerWithIdentifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎没有得到这个我不断得到的SIGABRT。我有这个故事板iOS应用程序,在故事板中我有一个UITableViewController。现在,我可以拿一个TVC的单元格并使其推动segue视图控制器,但是如果我需要在某些条件下停止segue动作呢?显然你不能,因为prepareForSegue:sender:方法不允许它,它似乎是转换即将执行时被调用的唯一回调。

I don't seem to get this SIGABRT I keep getting. I have this storyboard iOS application, and in the storyboard I have a UITableViewController. Now, I can take a cell of the TVC and make it push the "segue" view controller, but what if I needed to stop the "segue" action on certain conditions? Apparently you can't, since the prepareForSegue:sender: method doesn't allow for it, and it seems to be the only callback that gets called when a transition is about to get performed.

所以我猜我可以进入tableView:didSelectRowAtIndexPath:并以编程方式执行segue。不是最理想的,但仍然......

So I guessed I could go into the tableView:didSelectRowAtIndexPath: and perform the segue programmatically. Suboptimal, but still…

嗯,事实证明我错了。或者至少,我做错了什么。最明显的方法是

Well, it turns out I guessed wrong. Or at least, I'm doing something wrong. The most obvious way to do it would be

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"TheOtherIdentifier" sender:self];
}

但是整个应用程序崩溃了SIGABRT,它没有提供任何有用的信息(是的,我确定是那条使应用程序崩溃的行,我检查了调试器:)此外,我正在尝试加载的VC已正确设置标识符,因为以下代码

but the whole app crashes with a SIGABRT, which does not give any useful information (and yes, I'm sure it's that line that makes the app crash, I checked with the debugger :) Moreover, the VC I'm trying to load has the identifier correctly set, because the following code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"TheOtherIdentifier"];
    [self.navigationController pushViewController:vc animated:YES];
}

有效。引号表明这显然不是应该执行此类转换的方式。

"works". Quotation marks indicate that this is clearly not the way such a transition should be performed.

现在:想法?

推荐答案

试试这个:


  1. 使用第一个代码块而不是第二个代码块。

  2. 在storyboard控件中,从单元格拖动到另一个视图控制器。请注意,创建了一个segue。

  3. 单击segue。使用属性检查器给出segue和标识符theOtherIdentifier(建议使用小写t)。假设您正在使用导航控制器,也请选择推的segue样式。

  4. 故事板将实例化其他视图控制器。请确保您的代码中没有这样做。

  1. Use the first code block and not the second.
  2. In storyboard control drag from the cell to the other view controller. Note that a segue is created.
  3. Click on the segue. Use the attributes inspector to give the segue and identifier "theOtherIdentifier" (lower case "t" recommended). Also select a segue style of "push" assuming you are using a navigation controller.
  4. Storyboard will instantiate the other view controller. Be sure you are not doing this in your code.

这篇关于performSegueWithIdentifier vs instantiateViewControllerWithIdentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆