0

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]
}
Junaid Khan
  • 125
  • 2
  • 13
  • did you set the class to the delegate? – PaFi Apr 04 '19 at 09:58
  • Can you show coder where you added `AcceptedBookingVC` in `ContainerVC`? – Kamran Apr 04 '19 at 10:14
  • i have edited the code please do check. @Kamran – Junaid Khan Apr 04 '19 at 10:18
  • no i haven't set , i want to know how to set it in Child VC, because my Child VC i not loading through any name or segue , it loads on Container VC through XLPager Tab Strip method. @PaFi – Junaid Khan Apr 04 '19 at 10:19
  • You have to set delegate `child1.delegate = self`. Not enough code in question to understand how parent-child viewController setup is done. However you have to grab the reference to your `AcceptedBookingVC` controller and set its delegate to get changes. – Kamran Apr 04 '19 at 10:23
  • I have pass my child1 delegate but its showing me error, Value of type 'AcceptedBookingVC' has no member 'dateDelegate' . @Kamran – Junaid Khan Apr 04 '19 at 10:30

1 Answers1

0

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

self.dateDelegate = childVC
Ajay saini
  • 2,352
  • 1
  • 11
  • 25
  • In my parent class child VC is detect once in XLPagerTabStrip delegate, override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { let child1 = UIStoryboard().loadAcceptedBookingVC() self.dateDelegate = AcceptedBookingVC() child1.itemInfo = "Accepted" let child2 = UIStoryboard().loadPendingBookingVC() child2.itemInfo = "Pending" let child3 = UIStoryboard().loadHistoryBookingVC() child3.itemInfo = " History" return [child1,child2,child3] } . @Ajay saini – Junaid Khan Apr 04 '19 at 13:47
  • I have mention like this but it isn't catching delegate in AcceptedVC, let child1 = UIStoryboard().loadAcceptedBookingVC() self.dateDelegate = AcceptedBookingVC.self as? BookingContainerDelegate child1.itemInfo = "Accepted" – Junaid Khan Apr 04 '19 at 13:55
  • what you are doing is, you are instantiating a new child view controller which is not correct. – Ajay saini Apr 04 '19 at 14:18
  • you want this delegation in only one VC ? – Ajay saini Apr 04 '19 at 14:19
  • i want to pass date from Parent VC to Child VC . @Ajay saini – Junaid Khan Apr 04 '19 at 16:30