xmartlabs / XLPagerTabStrip

Android PagerTabStrip for iOS.
MIT License
6.97k stars 1.32k forks source link

Notification center addObserver being called more than once #758

Open hamzabinamin opened 4 years ago

hamzabinamin commented 4 years ago
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {

        let child1 = UIStoryboard.init(name: "Map", bundle: nil).instantiateViewController(withIdentifier: "MyVC") as! MyVC

        let child2 = UIStoryboard.init(name: "Map", bundle: nil).instantiateViewController(withIdentifier: "MyVC") as! MyVC

        let child3 = UIStoryboard.init(name: "Map", bundle: nil).instantiateViewController(withIdentifier: "MyVC") as! MyVC

        let child4 = UIStoryboard.init(name: "Map", bundle: nil).instantiateViewController(withIdentifier: "MyVC") as! MyVC

        return [child1, child2, child3, child4]
    }

I have 4 child view controllers inside the tab bar, Let's assume I call a second view controller from child1. I have NotificationCenter.default.post here in this second VC which calls a function in child1 through notification center's add observer. The problem is, the add observer function in child1 is being called more than once.

I believe it has something to do with the other children (child2, child3, child4) since originally they all are using the same VC but with different data so the observer is present in all of them but I only want it to execute from the current child (in our example child1) that opens up the second VC. If child 2 opens up second VC, then the observer function should execute in child2 only.

I have tried storing the VC from which second VC gets opened but that doesn't seem to work as well.

Any suggestions or how could I fix this?

hamzabinamin commented 4 years ago

@monoqlo @nayzak @danielpetroianu @OliverPearmain

Would appreciate if you guys can look into this.