subinspathilettu / SJSegmentedViewController

MIT License
334 stars 126 forks source link

viewWillAppear called for every tab at the same time #133

Open abhinav2014 opened 4 years ago

abhinav2014 commented 4 years ago

When i set segmentControllers = [viewController1, viewController2, viewController3] viewWillAppear is called for all the controllers at one time, i.e., all the controllers are loaded and shown at the same time, and not on selecting the tab. How can this be resolved that when i select a tab, then only viewWillAppear is called. Please help.

sivatejab commented 4 years ago

There is now way for viewWillAppear will be called because, they are added as subviews. I have achieved similar behavior with protocols. https://github.com/subinspathilettu/SJSegmentedViewController/issues/143

sudhakar-varma commented 4 years ago

Use the delegate method func didMoveToPage(_ controller: UIViewController, segment: SJSegmentTab?, index: Int) { if index == 0 { NotificationCenter.default.post(name: Notification.Name("yourViewController"), object: nil) }else if index == 1 { NotificationCenter.default.post(name: Notification.Name("yourViewController"), object: [nil]) } }

Put this notification in your viewwillappear NotificationCenter.default.addObserver(self, selector: #selector(updateCode), name: Notification.Name("yourViewController"), object: nil)

@objc func updateCode(notification: Notification) { //do what u need }