在XLPagerTabStrip中将数据从容器VC传递到子VC

时间:2019-04-04 09:52:08

标签: swift delegates xlpagertabstrip

我正在使用第三方库XLPagerTabStrip。我有一个容器VC和三个孩子VC。现在我有一个按钮,可以在我的Container VC中点击显示日历。我希望当我点击任何日期时,都应将该日期传递给我的Child VC,然后我将在提供的日期基础上命中一个API。我已经在Container VC中创建了一个协议,并将日期传递给它的函数,并在扩展中的Child VC中调用该委托,但是它没有捕获我的委托。这是我的Container VC类的代码,

protocol BookingContainerDelegate {

func selectedDate(selectedDate:String)
}

在这里,我将selectedDate传递给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()
    }
}

现在在我的Child VC中,这就是我调用委托并将数据从委托传递到我的变量的方式,

extension AcceptedBookingVC : BookingContainerDelegate

{

func selectedDate(selectedDate: String) {

    date = selectedDate
}
}

但是在加载视图时,它并没有捕获我的委托。我如何在XLPagerTabStrip的Child VC中传递此日期。 这就是我在Container VC中称呼Child 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 个答案:

答案 0 :(得分:0)

在您的父VC中检测到当前活动的子VC并将委托分配给子VC

self.dateDelegate = childVC