我正在尝试使用PageViewController生成新的ViewController,其数据基于UserDefaults中保存的名称数组。当用户将新名称添加到数组中时,PageView应该在每次滑动时生成一个新页面,直到索引大于等于array.count。
问题是,当数组中有5个项目时,我可以无限次滑动,索引对于前两页正常工作,但是每次下一次滑动均保持为1,因此它永远不会成为==或> =,以便它将返回Nil。该数组似乎是正确的,在打印时会打印5个名称,但是由于某种原因,它在pageView中索引不正确,任何人都可以找出问题所在吗?
我希望这是足够的信息,Manager是我持有称为钱币的数组的类,该数组目前包含5个加密货币名称,其想法是能够添加一个新页面,追加数组,以便pageView将生成一个新页面,该页面将从API检索数据以加载该硬币的特定数据,这与IOS天气应用程序的工作方式非常相似。谢谢您的帮助,这是pageViewController的代码:
import UIKit
class PageViewController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate {
//page Control dots
var pageControl = UIPageControl()
func configurePageControl() {
// The total number of pages that are available is based on how
many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = (Manager.shared.coins.count)
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.pageIndicatorTintColor = UIColor.gray
self.pageControl.currentPageIndicatorTintColor = UIColor.white
self.view.addSubview(pageControl)
}
// MARK: Delegate functions
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = (viewControllers?.index(of: pageContentViewController)!)!
}
func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> TemplateViewController? {
if Manager.shared.coins.count == 0 || index >= Manager.shared.coins.count {
return nil
}
let templateViewController = storyboard.instantiateViewController(withIdentifier: "templateController") as! TemplateViewController
templateViewController.dataObject = Manager.shared.coins[index]
print(Manager.shared.coins)
return templateViewController
}
func indexOfViewController(_ viewController: TemplateViewController) -> Int {
print(viewController.dataObject)
return Manager.shared.coins.index(of: viewController.dataObject) ?? NSNotFound
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = self.indexOfViewController(viewController as! TemplateViewController)
if (index == 0) || (index == NSNotFound){
return nil
}
index -= 1
return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = self.indexOfViewController(viewController as! TemplateViewController)
if index == NSNotFound {
return nil
}
index += 1
if index == Manager.shared.coins.count {
return nil
}
return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}
//List of View Controllers.
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
configurePageControl()
self.dataSource = self
if let firstViewController = viewControllerAtIndex(0, storyboard: self.storyboard!){
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
}
硬币:
class Manager {
var coins = [""]
let defaults = UserDefaults.standard
static let shared = Manager()
init() {
self.coins = self.defaults.stringArray(forKey: "SavedStringArray") ?? [String]()
}
func addCoin(coin:String) {
self.coins.append(coin)
self.defaults.set(self.coins, forKey: "SavedStringArray")
print ("coins from addCoin:")
print (self.coins)
}
}
然后我将硬币从其他地方添加到数组中,就像这样:
@IBAction func goButton(_ sender: Any) {
self.choosePerm()
Manager.shared.coins.append(chosenCoin)
Manager.shared.addCoin(coin: chosenCoin)
print(Manager.shared.coins)
print(chosenCoin)
}
编辑:上面我已经编辑了添加数组的方式,choosePerm()在搜索完成后获取了硬币的名称,而selectedCoin是搜索后选择的硬币,它肯定是1个名字,但是我是仍然每次都将其追加为两倍,您能看到其中可能引起该现象的任何内容吗?即它会打印
["Ethereum", "Boolberry", "Boolberry", "Bitcoin", "Bitcoin"]
答案 0 :(得分:0)
您要两次追加硬币:
Manager.shared.coins.append(chosenCoin) // <-- appending for the first time
Manager.shared.addCoin(coin: chosenCoin) // <-- appending for the second time
func addCoin(coin:String) {
self.coins.append(coin) // <-- this is the second appending
可以修复这种错误,但不允许从Manager
类外部进行添加:
private(set) var coins = [""]