How to segue with data from one Tab to another Tab properly
Asked Answered
Y

3

12

Storyboard Image

I have a Tab Bar Controller which is the Initial View Controller, which also has a PFLoginViewController that pups up if a user isn't logged in. The Login/Signup flow works fine.

The two tabs are 1. a UICollectionView which I will refer to as IntroVC from now on 2. a UITableView which I will refer to as FeedVC

When a user clicks a photo in IntroVC, a Show segue is triggered (via prepareForSegue) that shows a 3rd screen (UIView) which is technically not a tab. I will refer to this as the SelectVC from now on.

NOTE: All of these screens are also Embed(ded) In a Navigation Controller.

The SelectVC displays a photo, and there is a UIButton that the user can press which triggers a Show segue and Unwind segue, to push the image into the FeedVC. The reason I created an Unwind segue is because without it, the image would push into the FeedVC (2nd tab) but the first tab would still be highlighted.

I fixed this with the Unwind segue, but I noticed I'm having a problem where after a selection, when I press the 1st tab (Intro VC) the Nav bar has a Back button, and the more times I use the SelectVC button to push images, the more times I have to press Back in the IntroVC. I'm very confused about how to fix this. It's apparent that I'm not hooking up the flow properly and it seems that the IntroVC is being generated multiple times?

I get the following message in the console when I go through the segues in Simulator:

Nested pop animation can result in corrupted navigation bar

Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

Any help would be greatly appreciated!

Relevant code below.

IntroVC.swift

@IBAction func unwindToIntroView(segue: UIStoryboardSegue) {
    self.tabBarController!.selectedIndex = 1


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showFeedItem" {
        let selectScreenVC = segue.destinationViewController as! SelectScreenViewController
        let cell = sender as! UICollectionViewCell
        if let indexPath = self.collectionView!.indexPathForCell(cell) {
            self.navigationController?.popViewControllerAnimated(true)
            selectScreenVC.currentVenue = venueItems[indexPath.row]
        }
}

SelectVC.swift

@IBAction func pushSelection(sender: UIButton) {
    var feedItem = FeedItem()
    if let currentItem = currentItem {
        feedItem.nName = currentItem.nName
        feedItem.imageFile = currentItem.lgImg
        feedItem.userName = PFUser.currentUser()!.username!
        feedItem.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
            self.performSegueWithIdentifier("unwindToVenueView", sender: self)
        })
    }
}

I know this is weirdly structured, and if I'm missing information that is needed to fully understand - please let me know and I'll edit accordingly.

Yip answered 24/4, 2015 at 19:5 Comment(0)
N
5

Segue to another tab with data

This solution uses an unwind segue to switch to a new tab and send it data.

enter image description here

  • Green is Tab 1
  • Blue is a descendant of Tab 1
  • Yellow is Tab 2

Goal: Go from Blue to Yellow and pass data at the same time.

Code

Blue View Controller (Descendant of Tab 1)

import UIKit
class BlueViewController: UIViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // this is the data that we want to send
        let myData = "Hello from Blue"

        // Get a reference to the destination View Controller
        // and set the data there.
        if let yellowVC = segue.destination as? YellowViewController {
            yellowVC.data = myData
        }
    }
}

Yellow View Controller (Tab 2)

import UIKit
class YellowViewController: UIViewController {

    var data: String?

    @IBOutlet weak var dataLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

    @IBAction func comingFromBlueUnwindSegue(segue: UIStoryboardSegue) {
        // This may get called before the UI views have been loaded if
        // the user has not navigated to this tab before. So we need to make
        // sure that the label has been initialized. If it hasn't then we
        // will have our chance to call updateUI() in viewDidLoad().
        // We have to call it here too, though, becuase if the user has
        // previously navigated to this tab, then viewDidLoad() won't get
        // called again.
        if dataLabel != nil {
            updateUI()
        }
    }

    func updateUI() {
        // only update the label if the string data was previously set
        guard let myString = data else {return}
        dataLabel.text = myString
    }
}

Interface Builder

Control drag from the button to the Exit icon on the top of the source View Controller. Since we have already added the unwind segue code to the destination View Controller, it will show up as an option. Choose it.

enter image description here

Notes

  • Thanks to this answer for setting me on the right track.
  • You could also set the unwind segue by control dragging from the Exit icon to the View Controller icon. Then you could call the segue programmatically. See this answer for details.
  • There was no special code to show for the Tab 1 view controller.
Nuclide answered 11/12, 2017 at 11:28 Comment(0)
I
0

I think your problem comes in this line (prepareForSegue method)

self.navigationController?.popViewControllerAnimated(true)

since you are trying to pop the view controller from the stack before presenting SelectVC. This may well be the cause for a possible corrupted navigation bar (what if you pop the root view controller?). You can try the following method instead:

self.navigationController?.popToRootViewControllerAnimated(true)
Insecure answered 1/5, 2015 at 16:17 Comment(2)
hey @flizana, I tried your suggestion but this didn't work unfortunately. Thanks for trying to help!Yip
Have you tried putting breakpoints in every method called to check which line is throwing the exception?Insecure
D
0

I'm not sure I understand the reason you call self.navigationController?.popViewControllerAnimated(true) inside prepareForSegue: of IntroVC.swift: this way you "pop" the ViewController out of the stack even before presenting it (meaning that you actually attempt to pop IntroVC out of the stack, not SelectVC).

First thing I'd try, comment that line in prepareForSegue: and see what happens.

I can't try right now, but I wouldn't be surprised if calling self.performSegueWithIdentifier("unwindToVenueView", sender: self) in SelectVC.swift would automatically trigger the pop of itself, without needing to call it manually. If it isn't so, you can add popViewControllerAnimated (or maybe popToRootViewControllerAnimated) to pushSelection: of SelectVC.swift.

Let me know if this works!

Have a nice day,

@cdf1982

Dejected answered 3/5, 2015 at 10:33 Comment(2)
Hi @Dejected thanks for your advice. I tried your suggestions but unfortunately they didn't work. There is something I'm missing, but I can't figure out what. Again, thank you for your time!Yip
Don't mention it. Last suggestion: try to comment self.tabBarController!.selectedIndex = 1 in the unwind method: the error logged complains about an unexpected state when finishing the navigation. Maybe it's the unwind changing VC too soon, and even if it isn't, you probably want to add breakpoints to your code and follow the execution step by step to understand what prompts those two errors. Good luck!Dejected

© 2022 - 2024 — McMap. All rights reserved.