gpt4 book ai didi

ios - UIPageViewController 中的相同 UIViewController

转载 作者:行者123 更新时间:2023-11-30 11:15:00 25 4
gpt4 key购买 nike

我需要一个 UIPageViewController 来显示与数组中可用数据一样多的 UIViewController。假设我有一个 Int 数组,对于每个 Int,我希望 UIViewController 在 UILabel 中显示其中一个。为了实现这一点,我在主 Storyboard中创建了 UIPageViewController 和 UIViewController 及其 UILabel。

这是我为 UIPageViewController 编写的代码:

import UIKit
import RealmSwift

class PageViewController: UIPageViewController, UIPageViewControllerDataSource {

lazy var subViewControllers: [UIViewController] = {
return [
self.createNewVC(withName: "CounterViewController"),
self.createNewVC(withName: "CounterViewController"),
self.createNewVC(withName: "CounterViewController")
]
}()

var dataArray: [Int] = [30, 134, 345]

override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
}

// Creates a new ViewController
func createNewVC(withName name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}

// Returns the previous ViewController
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex: Int = subViewControllers.index(of: viewController) ?? 0

if (currentIndex <= 0) {
return nil
}

if let previous = subViewControllers[currentIndex - 1] as? CounterViewController {
previous.daysLeftLabel.text = String(dataArray[currentIndex])
return previous
} else {
return nil
}
}

// Returns the next ViewController
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex: Int = subViewControllers.index(of: viewController) ?? 0

if (currentIndex >= subViewControllers.count - 1) {
return nil
}

if let next = subViewControllers[currentIndex + 1] as? CounterViewController {
next.daysLeftLabel.text = "0"
return next
} else {
return nil
}
}

}

这是 UIViewController 的代码

import UIKit

class CounterViewController: UIViewController {

@IBOutlet weak var daysLeftLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

注意:“CounterViewController”也是 UIViewController StoryBoardID

问题是,当我第一次向右滚动时,应用程序在这一行崩溃 next.daysLeftLabel.text = "0" 说它在展开可选时发现了一个 nil 值。

我在这里做错了什么?

我也想借此机会问一件事。在这种情况下,正如您可能已经猜到的那样,我使用的是 UIPageViewController,它包含相同的 UIViewController,但显示不同的数据,这是实现此目的的最佳方法?我做得正确还是有更简单、更干净的方法?

最佳答案

这条线崩溃了

next.daysLeftLabel.text = "0"

因为 daysLeftLabel 在 VC 加载之前为零,所以您需要

class CounterViewController: UIViewController {
@IBOutlet weak var daysLeftLabel: UILabel!
var sendedText = ""
override func viewDidLoad() {
super.viewDidLoad()
daysLeftLabel.text = sendedText
// Do any additional setup after loading the view.
}

}

然后使用

next.sendedText = "0"

关于ios - UIPageViewController 中的相同 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com