Reputation: 1175
In my application I need that each controller showed popup when some event happens.
I can listen to this NSNotification in each controller. But I don't want to duplicate the code.
Is there a way to listen to NSNotification and do some actions globally?
Upvotes: 1
Views: 379
Reputation: 1049
I'd suggest you listen the notification in your AppDelegate
.
Once you have catch the notification, present the popup on the top most view controller.
You can determine the top most view controller as described in this question.
Or just use another third-party helper: PPTopMostController.
The following example uses PPTopMostController to determine the top most view controller:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserverForName:NotificationName
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Good News"
message:@"Your friend Ostap Maliuvanchuk sent you a gift!"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
// show the user the gift
// maybe open a new controller
}]];
[[UIViewController topMostController] presentViewController:alert
animated:YES
completion:nil];
}];
return YES;
}
Upvotes: 1
Reputation: 513
You can write a custom UIViewController that listens to notifications, and then make all other UIViewControllers subclass it. That way they all can listen, and you do not duplicate code. I think this should work:
class ListeningViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("derp"), name: "herp", object: nil)
}
func derp() {
print("herp-derp")
}
}
class FunctionalViewController: ListeningViewController {
override func derp() {
print("i can also derp")
}
}
or you can write a UIViewController extension:
extension UIViewController {
public func startListenintToNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("derp"), name: "herp", object: nil)
}
func derp() {
print("herp-derp")
}
}
And just call the startListeningToNotificationsMethod once in every ViewController
Upvotes: 1