Reputation: 408
I have a HomeViewController
that is opening a WelcomeViewController
using segue and is from type Sheet.
In WelcomeViewController
the User is entering information and then closing(dismissing) it. After WelcomeViewController
is closed I want to update HomeViewController
by changing the String
value of a TextField
.
The Problem is, that ViewDidAppear
in HomeViewController
is not called after dismissing WelcomeViewController
. I did not have this Problem before when developing for iOS.
How can I fix this? I tried to call it manually but got problems, because my IBOutlets
were set to nil and I could not change the text in my TextField
.
This is the code that invokes the segue:
self.performSegue(withIdentifier: "welcome", sender: self)
this is my viewDidAppear
in HomeViewController
:
override func viewDidAppear() {
super.viewDidAppear()
if (CoreDataAccess().countUsers() == 0) {
self.performSegue(withIdentifier: "welcome", sender: self)
} else {
//continue execution..
}
}
In WelcomeViewController
I dismiss the ViewController
using this code
dismiss(self)
Upvotes: 0
Views: 1733
Reputation: 408
This is how I solved it using a delegate
.
Thanks to @ullstrm for the suggestion.
First I added this protocol to HomeViewController
protocol WelcomeDelegate {
func insertData(_ name: String, birthday: Date)
}
Then this prepare(for segue:)
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destinationController as? WelcomeViewController {
destinationViewController.delegate = self
}
}
And this extension at the bottom (I know it's not ok to call viewDidAppear manually , but it's working for now)
extension HomeViewController : WelcomeDelegate {
func insertData(_ name: String, birthday: Date) {
viewDidAppear()
}
}
Then in WelcomeViewController
I added this variable
var delegate:WelcomeDelegate? = nil
And this line right before dismiss(self)
self.delegate?.insertData(tfName.stringValue, birthday: dpAge.dateValue)
Upvotes: 1
Reputation: 344
Add your code to ViewWillAppear() instead of ViewDidAppear().
or you call a method in the HomeViewController when dismissing WelcomeViewcontroller
dismiss(animated: true) {
self.presentedViewController.myMethod()
}
Upvotes: 0