ios performSegueWithIdentifier 与 instantiateViewControllerWithIdentifier

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8926606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:24:25  来源:igfitidea点击:

performSegueWithIdentifier vs instantiateViewControllerWithIdentifier

iosios5storyboardxcode-storyboard

提问by Morpheu5

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.

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

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

所以我猜我可以进入 tableView:didSelectRowAtIndexPath: 并以编程方式执行 segue。次优,但仍然......

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

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

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

- (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.

“作品”。引号表明这显然不是执行此类转换的方式。

Now: ideas?

现在:想法?

采纳答案by T.J.

Try this:

尝试这个:

  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.
  1. 使用第一个代码块而不是第二个。
  2. 在故事板控件中,从单元格拖动到另一个视图控制器。请注意,创建了一个 segue。
  3. 点击转场。使用属性检查器提供 segue 和标识符“theOtherIdentifier”(建议使用小写“t”)。假设您正在使用导航控制器,还可以选择“推送”的 segue 样式。
  4. Storyboard 将实例化另一个视图控制器。确保您没有在代码中执行此操作。