I have a Tab Bar Controller which is the Initial View Controller, which also has a P
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)
This solution uses an unwind segue to switch to a new tab and send it data.
Goal: Go from Blue to Yellow and pass data at the same time.
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
}
}
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.
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