ios - 如何将数据从数组传递到测验应用程序的多个标签?

标签 ios swift xcode

我应该创建测验应用程序,我刚刚创建了模板和 MVC,但无法理解如何将包含问题和答案的数组中的数据传递到屏幕上的标签? 我有3个屏幕 1.带有“开始”按钮的屏幕 2.主屏幕,我应该从数组传递数据 3.屏幕-结果

我附加了模板屏幕 enter image description here

struct Question {
    var text: String
    var answers: [Answer]
}

extension Question {
     static func loadData() -> [Question] {
        return [
            Question(
                text:some text",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text ?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "Сsome text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            )
        ]
    }
}

最佳答案

您可以将数据一一设置为标签,但这不是一个好的解决方案。如果您需要动态数量的答案,则传递数据就会变得复杂。

我建议使用TableView。您可以创建自定义单元格来显示答案并在表格 View 中使用它。 这是使用 TableView 的好例子:UITableView example for Swift

关于ios - 如何将数据从数组传递到测验应用程序的多个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990295/

相关文章:

iOS 如何在网络浏览器中实现选项卡

ios - iOS中如何获取调用方法的对象?

swift - 使用 NSDate 获取星期几

swift - 如何在不同情况下以编程方式快速限制 View 多次?

objective-c - 如何复制用户创建的表?

ios - 使用未解析的标识符 'FIRAuth'(Swift 2、Firebase 3.x)

php - 通过 PHP 联系页面从 iPhone 发送电子邮件

ios - 在 Swift 中减少托管对象数组

ios - tableViewCell 可以同时可移动并具有 .none 编辑风格的滑动操作吗

ios - 使用 iOS 7 SDK 编译遗留代码会破坏应用程序