SwiftUI:如何初始化依赖于视图中另一个变量的变量
我有一个页面,它应该得到一个自定义的“主题”(一个包含标题、背景图像和字符串数组中嵌入的问题列表的结构),然后我希望视图也有一个在卡片堆栈中显示的问题的无序列表,这应该是一个状态变量,这样我就可以用动画将卡片从列表中滑出 我一直试图在主体之前初始化问题数组,但遇到了属性初始值设定项中“self不可用”的问题。因此,我尝试在主体之前初始化问题变量,但这也会导致问题 然后,当我在代码中的其他地方调用此视图时,它希望我为currentQuestions参数而不是currentTopic参数传入一个值 我想链接到我刚刚传入一个currentTopic的视图,它基于当前主题创建一个无序数组,然后我可以显示它。有没有更好的方法来设置/初始化这些变量SwiftUI:如何初始化依赖于视图中另一个变量的变量,swiftui,Swiftui,我有一个页面,它应该得到一个自定义的“主题”(一个包含标题、背景图像和字符串数组中嵌入的问题列表的结构),然后我希望视图也有一个在卡片堆栈中显示的问题的无序列表,这应该是一个状态变量,这样我就可以用动画将卡片从列表中滑出 我一直试图在主体之前初始化问题数组,但遇到了属性初始值设定项中“self不可用”的问题。因此,我尝试在主体之前初始化问题变量,但这也会导致问题 然后,当我在代码中的其他地方调用此视图时,它希望我为currentQuestions参数而不是currentTopic参数传入一个值
struct TopicPage: View {
//inherit current topic from the PackTopics page, and create a state variable that gets set to a random question.
var currentTopic: Topic
@State var currentQuestions: [String]
init(currentQuestions: [String]) {
getQuestionList()
}
func getQuestionList() {
let newQuestionList: [String] = currentTopic.questions
currentQuestions = newQuestionList.shuffled()
}
var body: some View {
VStack {
Text(currentTopic.name).fontWeight(.bold)
.font(.title)
.padding(20)
.offset(x: 0, y: 50)
ZStack {
ForEach(0..<currentQuestions.count, id: \.self) { question in
QuestionBox(currentQuestion: self.currentQuestions[question])
.stacked(at: question, in: self.currentQuestions.count)
}
struct主题页:查看{
//从PackTopics页面继承当前主题,并创建设置为随机问题的状态变量。
var currentTopic:主题
@State var currentQuestions:[字符串]
初始化(currentQuestions:[字符串]){
getQuestionList()
}
func getQuestionList(){
让newQuestionList:[String]=currentTopic.questions
currentQuestions=newQuestionList.shuffled()
}
var body:一些观点{
VStack{
文本(currentTopic.name).fontWeight(.bold)
.font(.title)
.填充(20)
.偏移量(x:0,y:50)
ZStack{
ForEach(0..这里有一条路要走…因为@State
只能通过State
构造函数用init参数初始化
struct TopicPage: View {
//inherit current topic from the PackTopics page, and create a state variable that gets set to a random question.
var currentTopic: Topic
@State var currentQuestions: [String]
init(currentTopic, Topic, currentQuestions: [String]) {
self.currentTopic = currentTopic
// use this to initialize
self._currentQuestions = State(initialValue: currentTopic.questions.shuffled())
}
func getQuestionList() {
let newQuestionList: [String] = currentTopic.questions
// use this to update
self.currentQuestions = newQuestionList.shuffled()
}
这里有一个方法…因为只有通过状态
构造函数才能用init参数初始化@State
struct TopicPage: View {
//inherit current topic from the PackTopics page, and create a state variable that gets set to a random question.
var currentTopic: Topic
@State var currentQuestions: [String]
init(currentTopic, Topic, currentQuestions: [String]) {
self.currentTopic = currentTopic
// use this to initialize
self._currentQuestions = State(initialValue: currentTopic.questions.shuffled())
}
func getQuestionList() {
let newQuestionList: [String] = currentTopic.questions
// use this to update
self.currentQuestions = newQuestionList.shuffled()
}
谢谢-我遇到了一个问题,self.\u currentQuestions=State(currentTopic.questions.shuffled())给了我一个“调用初始值设定项时没有精确匹配”错误-知道是什么原因吗?谢谢-我遇到了一个问题,self.\u currentQuestions=State(currentTopic.questions.shuffled())给了我一个错误“调用初始值设定项时没有精确匹配”错误-知道是什么原因造成的吗?