简体   繁体   中英

Delegate stops being called swift

I have a delegate which I use to trigger next page of a Pageview controller and I have 6 viewcontrollers attached to the page viewcontroller. after the first 3 calls in 3 different controllers, the delegate stops getting called and as such, the next page of the page controller is not triggered, beleow is my code which works for first 3 and stops getting called after the first 3

This Button tap code is in 5 of the viewcontrollers with pageIndex set in each 1..5

   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)

My Protocol and Methods

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)
}

The problem may relates to the fact that you set a static index 1 here

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)

instead you need to have an index var in each vc and assign it when instantiate the vc so you can use it above or a vaibale in the main pager


Fix it

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)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM