Undistraction
Undistraction

Reputation: 43370

Can I trigger a segue from a UIBarButtonItem?

I have a toolbar in one of my storyboard's scenes. The toolbar contains a single UIBarButtonItem. I want a Touch Down on the button to trigger a segue to a new UIViewController. If I drag over to the viewController and choose 'Modal' from the popup, the segue is not triggered. 'prepareForSegue' is never called. In the Utility panel under 'Referencing Storyboard Segues' the Button has an exclamation mark next to its name and shows the following on rollover:

'null' is not a valid controller containment key path.

If I drag over from the button and select 'Push' from the popup, I do not get this warning , however again the segue doesn't work and prepareForSegue is never called.

If I hook up either type of segue to a UIButton (outside a Toolbar), the segue works fine in both cases. So it appears to me that Segues do not work when triggered from Toolbar items.

Can anyone confirm this or point out what I'm doing wrong?

Upvotes: 3

Views: 10341

Answers (4)

Darren
Darren

Reputation: 10398

You can call a segue yourself using

[self performSegueWithIdentifier: @"SegueToScene1" sender: self];

Upvotes: 20

Tim
Tim

Reputation: 5767

I think you may be using a UIButton instead of a UIBarButtonItem object. I was just having the same problem but I had initially dragged a standard button to the toolbar. When I changed it the storyboard segue worked as expected.

Upvotes: 4

Marco
Marco

Reputation: 6692

I set up a modal segue triggered from a UIBarButtonItem inside a UIToolbar in a sample project, it works fine.

Upvotes: 1

silverhand31
silverhand31

Reputation: 1

it's work with UIBarButtonItem, by using push. Did you test that without prepareForSegue, I mean, just link them in .storybar ? It would go to the linked ViewController, event without a line of code

Upvotes: 0

Related Questions