performSegueWithIdentifier vs instantiateViewControllerWithIdentifier
Asked Answered
A

1

16

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?

Autotomy answered 19/1, 2012 at 13:14 Comment(2)
Just to be clear, you said you set this identifier for the destination view controller. But did you set an identifier for the segue itself? That is the identifier you need to use in the first case.Diarmuid
Ok, I was missing that. Refer to T.J.'s answer for further comments.Autotomy
P
8

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.
Penetralia answered 19/1, 2012 at 14:55 Comment(5)
Thanks, that clarifies the issue, except that if I set the segue via IB, it gets performed no matter what. I'd need to include a condition, for example I need to check user's identity based on the values entered in other cells.Autotomy
Ok, sorry, I was missing the storyboard control part :) EDIT: Woops. Sorry again. I zoomed out in order to create the segue from one VC to the other, but I just realized I could do it dragging the top system bar. No worries, anyway: I succeeded :)Autotomy
Great. I'm glad that all worked. Creating a segue from the by select in the from view controller in the system bar is new to me. I used to create hidden buttons for conditional segue.Penetralia
iOS SDK usually does not make much sense to me but I always try to do things in a way that makes sense, and when I fail I just grunt and go their nonsensical way :) This time it worked, though :)Autotomy
Note: if you set the segue style to "push" and you're not embedded inside a valid UINavigationController ... Apple will silently do nothing :).Equiangular

© 2022 - 2024 — McMap. All rights reserved.