Pass Data from Container VC to Child VC in XLPagerTabStrip

389 Views Asked by At

i'm using a third party lib XLPagerTabStrip. I have a Container VC and Three Child VC. Now i have a button that display calendar on tap in my Container VC. I want that when i tap any date it should pass that date to my Child VC and there i will hit an API on the basis of provided date. I have made a protocol in my Container VC and pass date to its function and Call that delegate in my Child VC in an extension but it does not catch my delegate. Here is a code for my Container VC class,

protocol BookingContainerDelegate {

func selectedDate(selectedDate:String)
}

Here i'm passing selectedDate to delegate function in My COntainer VC,

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {

    let selected = calendar.selectedDate

    let dateFormatter1: DateFormatter = DateFormatter()
    dateFormatter1.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
    dateFormatter1.dateFormat = "yyyy-MM-dd"
    print("\(dateFormatter1.string(from: selected!))")

    dateDelegate?.selectedDate(selectedDate: dateFormatter1.string(from: selected!))

    self.popViewTopConstraint.constant = -300
    popUpShowing = false

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

Now in my Child VC , this is how i'm calling delegate and passing data from delegate to my variable,

extension AcceptedBookingVC : BookingContainerDelegate

{

func selectedDate(selectedDate: String) {

    date = selectedDate
}
}

But it is not catching my delegate, when the view had been loaded. How i can pass this date in my Child VC in XLPagerTabStrip. This is how i call Child VC in Container VC,

 override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    let child1 = UIStoryboard().loadAcceptedBookingVC()
    child1.itemInfo = "Accepted"

    let child2 = UIStoryboard().loadPendingBookingVC()
    child2.itemInfo = "Pending"

    let child3 = UIStoryboard().loadHistoryBookingVC()
    child3.itemInfo = " History"

    return [child1,child2,child3]
}
1

There are 1 best solutions below

5
Ajay saini On

in your parent VC detect your current active child VC and assign the delegate to child VC

self.dateDelegate = childVC