Ios 如何随机化页面顺序
我正在开发一个quote应用程序,我希望除了第一个以外的页面都是随机的。我不想让打开应用的人一次又一次地看到相同的初始报价。如果你能对代码进行编辑,我将非常感激。提前谢谢你Ios 如何随机化页面顺序,ios,iphone,swift,Ios,Iphone,Swift,我正在开发一个quote应用程序,我希望除了第一个以外的页面都是随机的。我不想让打开应用的人一次又一次地看到相同的初始报价。如果你能对代码进行编辑,我将非常感激。提前谢谢你 import UIKit class ModelController: NSObject, UIPageViewControllerDataSource { let pageData:NSArray = ["All quotes are from Kanye West, Enjoy!", "I refuse to
import UIKit
class ModelController: NSObject, UIPageViewControllerDataSource {
let pageData:NSArray = ["All quotes are from Kanye West, Enjoy!", "I refuse to accept other people's ideas of happiness for me. As if there's a 'one size fits all' standard for happiness","I am the greatest","I still think I'm the greatest.","They say you can rap about anything except for Jesus, that means guns, sex, lies, video tapes, but if I talk about God my record won't get played Huh?"]
override init() {
super.init()
}
func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> DataViewController? {
// Return the data view controller for the given index.
if (self.pageData.count == 0) || (index >= self.pageData.count) {
return nil
}
// Create a new view controller and pass suitable data.
let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! DataViewController
dataViewController.dataObject = self.pageData[index] as! String
return dataViewController
}
func indexOfViewController(_ viewController: DataViewController) -> Int {
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return pageData.index(of: viewController.dataObject)
}
// MARK: - Page View Controller Data Source
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = self.indexOfViewController(viewController as! DataViewController)
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! DataViewController)
if index == NSNotFound {
return nil
}
index += 1
if index == self.pageData.count {
return nil
}
return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
您可以随机化报价数组并传递随机化数组。 签出以下链接: 和
为什么要使用整个页面,而不是只更改一个页面的内容?