I am using: XCode 8.2, Swift 3.0, iOS 10.2, iPhone 7. I have a Tab Bar Controller
with child View Controllers
.
I am trying to send data from one view controller to another.
I've looked up how to do this here and here but the first link isn't quite what I want since he is talking about Storyboard segues which doesn't appear to be the same type of segues used when moving between tabs.
The tab in which I am trying to send data:
protocol SendResultsDelegate {
func sendResults(data: String)
}
class SendingVC {
....
if delegate != nil {
let data = // some string here
delegate?.sendResults(data: data!)
}
}
The tab in which I am trying to receive data and print it out:
import UIKit
class ResultsViewController: UIViewController, SendResultsDelegate {
@IBOutlet weak var resultsTextField: UITextView!
func sendResults(data: String) {
resultsTextField.text = data
}
}
And I am not sure if this is correct but...I also have a custom class attached to my Tab Bar Controller:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let resultsIndex = 2
if item == (self.tabBar.items! as [UITabBarItem])[resultsIndex] {
let sendingVC : SendingVC = segue.destinationViewController as! SendingVC
SendingVC.delegate = self
}
}
I'm not sure if it should be the tabBar
function or the prepare
function. I'm basically trying: when the user clicks the results tab, that delegate is set and then passes through the if
statement check on the ResultsViewController
which can display the text field. Of course, this fails to even compile/build and I am unsure where to go from here. Any help would be greatly appreciated.