为 Swift Xcode 中的每个数组项生成一个单独的页面

Generating a separate page for each array item in Swift Xcode

当我使用代码的第一部分时,我得到两个单独的页面,第 1 页为“运行 1”,第 2 页为“运行 2”。

let pages: [String] = ["Run1","Run2"]
            
            for text in pages {
                let vc = TextViewController(with: text)
                myControllers.append(vc)
            }

然而,当我更新数组以从我的结构文件中取回我的数组时,我只得到一个包含两个 运行 标题的页面,即“运行1运行 2”

let pages: [String] = [run.runTitle]
            
            for text in pages {
                let vc = TextViewController(with: text)
                myControllers.append(vc)
            }

我希望每个 运行 标题都有单独的页面,就像我在第一部分中看到的那样,我做错了什么?

附加代码:

import UIKit

var runs = [Run]()

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    // return the VC to be shown based on current position
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            // find index in array
            guard let index = myControllers.firstIndex(of: viewController), index > 0 else {
                return nil
            }
            let before = index - 1
            
            return myControllers[before]
        }
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
                // find index in array
            guard let index = myControllers.firstIndex(of: viewController), index < (myControllers.count - 1) else {
                    return nil
                }
                let after = index + 1
                
                return myControllers[after]
        }
        
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
         
            DispatchQueue.main.asyncAfter(deadline: .now(), execute: { self.presentPageVC()})
        }

        // create property of array objects
        var myControllers = [UIViewController]()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            if let savedRuns = Run.loadRuns() {
                runs = savedRuns
            }
            
            let pages: [String] = [run.runTitle]
            
            for text in pages {
                let vc = TextViewController(with: text)
                myControllers.append(vc)
            }
        }

        func presentPageVC() {
            guard let first = myControllers.first else {
                return
            }
            
            let vc = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
            
            // set up datasources
            vc.delegate = self
            vc.dataSource = self
            
            vc.setViewControllers([first], direction: .forward, animated: true, completion: nil)
            
            present(vc, animated: true)
        }

    }

let pages: [String] = [run.runTitle] 只会 return 一个只有一个字符串的数组。

您有 savedRunsrun,这是 运行 objects 的数组。

您需要遍历 运行 objects 的数组并从那里获取信息。

比如想获取运行个标题的数组,可以使用map方法来帮助(documentation on map)

let pages: [String] = runs.map({[=10=].runTitle})

这将 return 一个字符串数组。它基本上遍历 运行s 数组和 returns 每个 run.runTitle.

的数组

这与此相同:

var pages: [String] = []
for run in runs {
    pages.append(run.runTitle)
}

现在,如果您想取回多个项目,您可以这样做:

let pages: [String] = runs.map({[=12=].runTitle + [=12=].runNotes})

or

var pages: [String] = []
for run in runs {
    pages.append(run.runTitle + run.runNotes)
}