performSegueWithIdentifier vs instantiateViewContr

2019-01-22 03:53发布

问题:

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.

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

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.

Now: ideas?

回答1:

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.