Aviel Gross
Aviel Gross

Reputation: 9965

Elegant way to connect multiple views to one segue

In my storyboard I have a view controller (Call it "source") that push-segues to another view controller (let's say "destination").

The push can be triggered from one of three places:

  1. A UIButton inside a table view header
  2. One of the prototype cells (in cell selected...)
  3. one of the cells of a collection view that is inside a different type of prototype cell

What I currently have is segues that I ctrl+dragged from each of these views to the destination VC, and gave all of them the same identifier (since they all push the same VC...) This works flawlessly, except this annoying little thing that shows up every time I hit cmd+R:

enter image description here

Well, I tried to be a good citizen and anchor all my vies to an IBAction in the source VC where I call performSegueWithIdentifier: but I can't do it to the cell and the cel inside the election view... Only to the button in my header view...

I really don't want to have a code in my collection view custom class that call to the action method of the source view controller AND more code in didSelectCell... to check if it's the right cell prototype and than call the action method.... all this feels way worst than having 3 segues with the same id - which (in my opinion) really makes sense in my case - and also works perfect...

Just to make it clear - this is how it currently looks:

enter image description here

Any ideas?

Upvotes: 0

Views: 152

Answers (1)

jrturton
jrturton

Reputation: 119242

You can stick with your three segues pattern, but give each one a separate but related identifier, like PushDetailButton, PushDetailCell and so on.

In prepareForSegue, you can use hasPrefix:@"PushDetail" instead of isEqual to match any segue whose identifier starts with the string, instead of matching the whole string.

Upvotes: 1

Related Questions