0

I have a strange problem. I have two segues triggered by buttons in a table view controller, which is the root view controller of a navigation controller. One segue will trigger, and the other will not. They are from two different buttons, with two different identifiers.

I put a breakpoint inside prepare(for segue: UIStoryboardSegue, sender: Any) and I can verify that the second button is not calling it. Any ideas would be appreciated.

My code is:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "locationViewSegue", let destinationViewController = segue.destination as? LocationViewController  {
            destinationViewController.delegate = self
            destinationViewController.weatherData = self.weatherData
            destinationViewController.wxObservationStationsArray = self.wxObservationStationsArray
            destinationViewController.newCoordinates = self.weatherData.locationCoordinates
        } else if segue.identifier == "searchPriorLocations", let destinationViewController = segue.destination as? SearchBarTableViewController  {
            destinationViewController.priorForecastLocations = self.priorForecastLocations

        }
    }

My Storyboard follows with the offending segue highlighted: Storyboard

Any thoughts on this would be appreciated.

Jake
  • 13,097
  • 9
  • 44
  • 73
Yrb
  • 8,103
  • 2
  • 14
  • 44
  • Did you see [here](https://stackoverflow.com/questions/21619139/prepareforsegue-is-not-getting-called-when-i-click-on-a-button-without-using-per/40050522#40050522) – mfaani Jan 06 '17 at 14:28
  • Could you write a print inside the prepare(for segue: ...) method to see if it is being called by the button action ? – kevinrodriguez-io Jan 06 '17 at 14:29
  • You've shown us two segues but only one segue by IB definition. I'd focus on that. (1) Have you set a breakpoint for the "else" clause (segue == "searchPriorLocations")? Was it hit? (2) Any errors? Where is the code for the "second button"? –  Jan 06 '17 at 14:31
  • Wouldn't make much of a difference but did you drag them both from the **button** or from the **viewController** itself? – mfaani Jan 06 '17 at 14:32
  • 1
    @Honey That would make _all_ the difference. – matt Jan 06 '17 at 19:42
  • @matt knew something was wrong but didn't know what exactly... – mfaani Jan 06 '17 at 19:49
  • @darkndream & did: I put the breakpoint at the if statement. If prepare(for segue is called, the breakpoint is triggered. I checked it with the other button. It was never called, no errors. – Yrb Jan 07 '17 at 20:11
  • @Honey: Thanks for the link. It didn't fix it, but it was informative. I did drag both from the button to the viewController, and I did it again just before posting this. No joy. – Yrb Jan 07 '17 at 20:18
  • @dfd sorry about the typo above. Sometimes you gotta hate autocorrect. – Yrb Jan 07 '17 at 20:32

1 Answers1

3

I have two segues triggered by buttons

I'm going to suggest that in fact no, you don't. One of the segues is perhaps triggered by a button; that's the one that works as you expect — the segue fires when the button is tapped. But the other segue, though it exists, does not emanate from the other button; it emanates from the cell or from the view controller as a whole.

The way to check this is to select the button and display the Connections Inspector:

enter image description here

If you don't see that — an "action" triggered segue associated with the button — tapping the button won't fire the segue.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    @Honey the cell itself is tappable (selectable) so it, too, can have an action segue: you tap a cell and you segue. Surely you've seen that. – matt Jan 06 '17 at 21:41
  • @matt: The IB that I showed is the segue that is not working. I did check the connections Inspector and it shows it as a trigger segue to the SearchBarTableViewController. When I mouse over the button, the segue gets highlighted as well as the VC. Also, I don't trigger it from any cell, just the button. Triggering it from a cell will be a later experiment. :) – Yrb Jan 07 '17 at 20:24
  • @Yrb Unfortunately I can't see your whole storyboard / code from here. If you'd like me to look at your project, post it somewhere I can download it. I'll be glad to help further if I can. – matt Jan 07 '17 at 21:08
  • @matt Thank you for the very kind offer: https://app.box.com/s/50w6sd8pbtqqoy7r00ed32eek5x0wzjq It is ugly, but it is just for learning. – Yrb Jan 07 '17 at 22:37
  • So it looks to me, at first glance, like the problem is that the Search bar button item, from which the segue emanates, contains another object, a button whose title is a magnifying glass. So when you tap, you are tapping that magnifying glass button. But the segue does not emanate from that magnifying glass button; it emanates from the Search bar button item. The segue needs to emanate from the magnifying glass button because that is what you are tapping. So, to test that theory, you would need to drag out a segue from the magnifying glass button. – matt Jan 07 '17 at 23:17
  • @matt The magnifying glass was simply an emoji used as the title of the button. Changing the title did nothing. However, I deleted the button, and inserted a new bar button item. Not even changing the title, I hooked up the segue and low and behold it worked. I wish I had thought of that sooner. It never entered my thinking that the button itself was bad. . . aaarrrrggghhhh! Thanks for the assistance. I will mark this as solved. p.s. Changed the title to the magnifying glass and it worked. Something else, the button shrank to the size of the magnifying glass, where it didn't before. – Yrb Jan 08 '17 at 16:11
  • Right, because you've eliminated the extra interior button, which you never needed in the first place. – matt Jan 08 '17 at 16:30