0

I am trying to addObserver to all my views, when I start my application. When there is a post coming I want to display a Modal View on top of the current ViewController.

Is there a way to install it directly on every View or do I need to do the

viewWillAppear : add
viewDidDisappear : remove

workaround each time ?

JVS
  • 1,994
  • 3
  • 16
  • 28
  • You could make all your UIViewController inherits from a UIViewController that will add/remove the observing and even show the modal view. – Larme Oct 07 '16 at 15:22

2 Answers2

1
  1. You could have created one superclass for all you view controllers and override viewWillAppear/viewDidDisappear there.
  2. If there is no exception and you want to present a modal view controller no matter what view controller is currently on screen, you can present it over self.window.rootViewController in AppDelegate's didReceiveRemoteNotification method.
alexburtnik
  • 7,243
  • 4
  • 27
  • 63
  • 1
    You can also swizzle `viewDidAppear:` and/or `viewWillDisappear:`. I'd probably use option 1 myself, but it is an option. – AdamPro13 Oct 07 '16 at 15:29
1

Create parent class like this and subclass all other classes

import UIKit

class TemplateClassVC: UIViewController {


override func viewWillAppear() {
}

override func viewDidDisappear() {
}


}

and find top viewcontroller like this Get top most UIViewController

Community
  • 1
  • 1
neprocker
  • 171
  • 4