代表不再被称为 swift
Posted
技术标签:
【中文标题】代表不再被称为 swift【英文标题】:Delegate stops being called swift 【发布时间】:2019-06-23 20:51:37 【问题描述】:我有一个委托,用于触发 Pageview 控制器的下一页,并且我有 6 个视图控制器连接到页面视图控制器。在 3 个不同的控制器中的前 3 次调用之后,委托停止被调用,因此,页面控制器的下一页没有被触发,下面是我的代码,它适用于前 3 个并且在前 3 个之后停止被调用
此按钮点击代码位于 5 个 viewcontrollers 中,pageIndex 设置在每个 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)
我的协议和方法
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)
【问题讨论】:
【参考方案1】:问题可能与您在此处设置静态索引 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)
【讨论】:
没有办法获取MainVC中每个索引的值以上是关于代表不再被称为 swift的主要内容,如果未能解决你的问题,请参考以下文章
swift算法:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。