我有一个委托,我用它来触发Pageview控制器的下一页,并且我有6个viewcontroller附加到页面viewcontroller。在3个不同的控制器中进行前3次调用之后,委托停止被调用,因此,不会触发页面控制器的下一页,beleow是我的代码,它对前3个有效,在前3个后停止调用
此按钮的点击代码位于5个视图控制器中,并且在每个1..5中设置了pageIndex
weak var delegate: NextDelegate?
nextBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.next(pageIndex: 1)
}).disposed(by: disposeBag)
backBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.previous(pageIndex: 1)
}).disposed(by: disposeBag)
我的协议和方法
lazy var controllers: [UIViewController] = {
let locVC = LocationVC()
locVC.delegate = self
let typeVC = TypeVC()
typeVC.delegate = self
let descVC = DescVC()
descVC.delegate = self
let priceVC = PriceVC()
descVC.delegate = self
let featuresVC = FeaturesVC()
featuresVC.delegate = self
let picturesVC = PicturesVC()
picturesVC.delegate = self
return [locVC,
typeVC, descVC, priceVC, featuresVC, picturesVC]
}()
func backBtnClicked(index: Int) {
guard index - 1 >= 0 else { return }
pageController.setViewControllers([controllers[index - 1]], direction: .reverse, animated: false, completion: nil)
}
func nextBtnClicked(index: Int) {
log("\(controllers.count)", .happy)
guard index + 1 < controllers.count else { return }
pageController.setViewControllers([controllers[index + 1]], direction: .forward, animated: false, completion: nil)
}
extension ViewController: NextDelegate {
func next(pageIndex: Int) {
print("nexteddddd \(pageIndex)")
nextBtnClicked(index: pageIndex)
}
func previous(pageIndex: Int) {
print("backedddd \(pageIndex)")
backBtnClicked(index: pageIndex)
}
}
protocol NextDelegate: AnyObject {
func next(pageIndex: Int)
func previous(pageIndex: Int)
}
答案 0 :(得分:0)
问题可能与您在此处设置静态索引1的事实有关
nextBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.next(pageIndex: 1)
}).disposed(by: disposeBag)
backBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.previous(pageIndex: 1)
}).disposed(by: disposeBag)
相反,您需要在每个vc中都有一个索引var,并在实例化vc时对其进行分配,以便可以在上方使用它,也可以在主寻呼机中使用vaibale
修复
var current = 0 // assuming you set first vc initially
nextBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.next(pageIndex:self.current)
}).disposed(by: disposeBag)
backBtn.rx.tap.asDriver().drive(onNext: {
guard let delegate = self.delegate else {return}
delegate.previous(pageIndex:self.current)
}).disposed(by: disposeBag)
func backBtnClicked(index: Int) {
guard index - 1 >= 0 else { return }
self.current = index - 1
pageController.setViewControllers([controllers[index - 1]], direction: .reverse, animated: false, completion: nil)
}
func nextBtnClicked(index: Int) {
log("\(controllers.count)", .happy)
guard index + 1 < controllers.count else { return }
self.current = index + 1
pageController.setViewControllers([controllers[index + 1]], direction: .forward, animated: false, completion: nil)
}