0

My question is very similar to : Swift: Make button trigger segue to new scene

The issue is:

I have a view controller, with a button that causes another view controller to appear modally.

I have Ctrl+Click from the button to the second View Controller, and created the segue in IB.

Then I Ctrl+Click from the button again to the source code of the view controller to create an IBAction method.

I assumed that the button will do two things now: a) call the IBAction method , and b) perform the segue.

What happens is only the segue for some reason.

When I delete the segue, or remove the call to the view controller from IB, then the IBAction is called, but Xcode tells me that the second view controller is not reachable now.

I want to be able to present an ActionSheet to the user and then be able to performSegue to the second view controller, based on what the user selected from the action sheet.

I know I can programatically call performSegue but that requires the creation of the segue and attaching it to a physical button in IB, which defeats the purpose of not calling the IBAction that button may already have.

Community
  • 1
  • 1
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • One of the many reasons I hate storyboards (personally I don't use them). Can you post a picture of it? – Aggressor Jul 08 '15 at 22:25
  • Also need to see the IBActions you have for the button and segue – Aggressor Jul 08 '15 at 22:25
  • @Aggressor Thanks , but I already figured it out. Thanks to the answer below from knutigro – Ahmad Jul 08 '15 at 22:40
  • related: http://stackoverflow.com/q/21205485/2792531 (note this comment: http://stackoverflow.com/questions/21205485/ios7-segue-and-storyboards-how-to-create-without-a-button/21205550#comment31933618_21205550) – nhgrif Jul 08 '15 at 23:00

2 Answers2

6

If I want to do some additional steps before calling the segue I usually attach an IBAction to the UIButton and call the perform segue from within this, in the code. You can add a segue to the storyboard and give it an ID, without having to connect it to a button. You do this by control-clicking on the viewcontroller and drag to the next viewcontroller in the storyboard.

knutigro
  • 1,082
  • 2
  • 10
  • 20
2

Connecting a segue from storyboard will automatically always perform the segue. You can do any preparation for transitioning to the new view controller in the prepareForSegue method.

If there is some logic that comes before, like a user selecting something from the action sheet, just use the IBAction and then perform the segue with performSegueWithIdentifier based on the user selection. However, you do not have to create another button, just ctrl+drag from one view controller to another and give the segue an identifier.

Max
  • 1,143
  • 7
  • 7