Reputation: 199
How can I move the data stored in data
into the next VC and append it in my list when the sendDate
is tapped ? Here is my code of the sending class :
protocol DataSentDelegate{
func userDidEnterData(data: String)
}
class SecondViewController: UIViewController{
var delegate: DataSentDelegate!
@IBAction func addItem(_ sender: Any)
{
let data = textField.text
delegate?.userDidEnterData(data: data)
}
Here is the code of recieving class :
class SecondPageViewController: UIViewController, DataSentDelegate{
func userDidEnterData(data: String) {
}
@IBAction func sendDate(_ sender: UIDatePicker) {
}
How do I implement list.append(data!)
where data
holds the value of textField.text
Upvotes: 0
Views: 79
Reputation: 1060
You can share data through whole project using Singleton Pattern.
A singleton class is initialised for only once.
Look at these answers:
Swift - set delegate for singleton
Upvotes: 1
Reputation: 345
Do you not have to set the delegate in SecondPageViewController to self (normally in ViewDidLoad?
You declared it but don't seem to have assigned it.
Upvotes: 1
Reputation: 4669
What about you just add an array variable in your SecondPageViewController
which will hold a list of strings, then append a new string each time sendDate
delegate method gets called?
A few other remarks, there is no need to declare your delegate var as implicitly unwrapped if you're using optional chaining anyway, just declare it as an optional. Secondly, because SecondPageViewController
is a class, it's better to make your delegate protocol class bound as such: protocol DataSentDelegate: class { func userDidEnterData(data: String) }
, thirdly to avoid possible strong reference cycles make your delegate var a weak one.
Upvotes: 0