我正在使用第三方库 XLPagerTabStrip。我有一个容器 VC 和三个子 VC。现在我有一个按钮,可以在 Container VC 中点击显示日历。我希望当我点击任何日期时,它应该将该日期传递给我的 Child VC,然后我将根据提供的日期点击 API。我在我的容器 VC 中制定了一个协议(protocol),并将日期传递给它的函数,并在扩展中调用我的子 VC 中的委托(delegate),但它没有捕获我的委托(delegate)。这是我的 Container VC 类的代码,
protocol BookingContainerDelegate {
func selectedDate(selectedDate:String)
}
这里我将 selectedDate 传递给 My COntainer VC 中的委托(delegate)函数,
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 中,这就是我调用委托(delegate)并将数据从委托(delegate)传递到我的变量的方式,
extension AcceptedBookingVC : BookingContainerDelegate
{
func selectedDate(selectedDate: String) {
date = selectedDate
}
}
但是当 View 加载时,它没有捕获我的委托(delegate)。我如何在 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]
}
最佳答案
在您的父 VC 中检测当前事件的子 VC 并将委托(delegate)分配给子 VC
self.dateDelegate = childVC
关于swift - 将数据从容器 VC 传递到 XLPagerTabStrip 中的子 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55512930/