0

In my ViewControllerA, I try to show ViewControllerB by calling:

let VC2 = ViewControllerB()
VC2.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
presentViewController(VC2, animated: true, completion: nil)

So the content of ViewControllerB is shown on top of ViewControllerA.

When pressing a button in ViewControllerB, this is called:

dismissViewControllerAnimated(true, completion: nil)

However, viewWillAppear of ViewControllerA is not called.

If the line VC2.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext is removed, then viewWillAppear of ViewControllerA is called.

While using UIModalPresentationStyle.OverCurrentContext, viewWillAppear of ViewControllerA is not called. How to detect if ViewControllerB is dismissed in ViewControllerA in this situation? I want to run some codes in ViewControllerA, but not using completion of dismissViewControllerAnimated in ViewControllerB.

ehhhuang
  • 117
  • 1
  • 3
  • 9

1 Answers1

2

Why don't you create your own completion block on ViewControllerB? You can assign it when you create the instance of ViewControllerB on ViewControllerA, then you can just call that when you call dismissViewControllerAnimated on ViewControllerB.

Matthew Hallatt
  • 1,310
  • 12
  • 24
  • Thank you for the answer. Unfortunately, I am unfamiliar with completion blocks. I have spent a few hours searching online but still can't figure out what to do. Can you please show me an example or some hints where I should start? Many thanks! – ehhhuang Nov 02 '15 at 15:09
  • There are plenty of answers here on SO that should help you :) Like [this](http://stackoverflow.com/questions/16324095/custom-completion-block-for-my-own-method) or [this](http://stackoverflow.com/questions/4081831/how-to-store-blocks-in-properties-in-objective-c) – Matthew Hallatt Nov 02 '15 at 16:32
  • Thank you for the links in Objective-C. They helped me to understand completion blocks. Now I have got a solution in Swift. [Here](http://studyswift.blogspot.tw/2015/11/using-completion-block-to-detect.html) is it as well as some references. – ehhhuang Nov 03 '15 at 14:27
  • @ehhhuang You're welcome! Glad you got your issue sorted. Care to accept my answer? – Matthew Hallatt Nov 03 '15 at 17:20
  • Your answer is really helpful to me. I just learned what "accept answer" means in SO. :) – ehhhuang Nov 04 '15 at 00:23