0

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)
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Alexander Maslew
  • 408
  • 5
  • 16
  • Please check this thread https://stackoverflow.com/questions/29271880/viewdidappear-not-called-when-the-modal-view-is-dismissed – Natarajan Nov 07 '18 at 14:30
  • Post the code that creates your view controller/invokes the segue, and also post your viewDidAppear method. (you should never call that method. That's what hte system does.) – Duncan C Nov 07 '18 at 14:34
  • @DuncanC I posted the code – Alexander Maslew Nov 07 '18 at 15:08
  • 1
    Use a delegate. viewDidAppear will not be called in your case – ullstrm Nov 07 '18 at 15:17
  • Are you doing anything in `prepare(for segue:)`? Maybe passing the instance of `HomeViewController`? Also, I've never used a "Sheet" type segue but why to create an unwind segue? –  Nov 07 '18 at 15:21
  • 1
    Your outlets should not be nil. how are you trying to pass the data from welcomeViewController back to first screen?you need to show more code. your problem is not clear – Scriptable Nov 07 '18 at 15:22
  • @dfd no, I don't have a prepare(for segue:) – Alexander Maslew Nov 07 '18 at 15:26
  • @Scriptable I am using CoreData to save the data. And then display it in homeViewController – Alexander Maslew Nov 07 '18 at 15:26
  • @Scriptable I created the segue in my Storyboard and calling it with the identifier "welcome" manually from code... Other code is only saving and loading from CoreData and is not related to my Problem. – Alexander Maslew Nov 07 '18 at 15:36
  • I waited for others more experienced than me, and nothing. So. From my perspective? You haven't given us enough to help. My questions? Ok... you didn't see a need for `prepare(for: segue:)`... why? That **a** standard way for pass the *source* view controller to it's *target*. You never answered @ullstrm... why not try using a delegate? It works *very* well when *presenting* another view controller form one. Finally, did @Kevinosaurio help? Or at least give you the correct direction? This is now on **you** to provide something more.... –  Nov 07 '18 at 20:24
  • I just found the solution using a delegate. As @ullstrm said, in my case viewDidAppear will not be called. Using the delegate my textfields were not set to nil anymore. Will post the solution now.. I did not see the need for prepare, because I don't pass any data to my second ViewController. – Alexander Maslew Nov 07 '18 at 21:19

2 Answers2

1

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)
Alexander Maslew
  • 408
  • 5
  • 16
0

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()
}
Raul Mantilla
  • 334
  • 1
  • 5
  • Not working, ViewWillAppear() is also not called after i dismiss my second ViewController. – Alexander Maslew Nov 07 '18 at 15:10
  • call a method in the HomeViewController when dismissing WelcomeViewcontroller dismiss(animated: true) { self.presentedViewController.myMethod() } let me know.. – Raul Mantilla Nov 07 '18 at 15:14
  • Yes, I can call a method, but this does not help me get access to my TextField. Without calling ViewDidAppear my IBOutlets are set to nil. – Alexander Maslew Nov 07 '18 at 15:19