【问题标题】:Generate random text from JSON file when button is pressed - SwiftUI按下按钮时从 JSON 文件生成随机文本 - SwiftUI
【发布时间】:2022-11-14 05:03:59
【问题描述】:

我想从按钮调用的 json 文件创建随机文本生成器。 此时我有随机文本生成器,但要刷新我需要返回另一个视图并打开相同的视图。

我怎样才能让它通过按钮刷新? 我曾尝试通过函数创建它,但每次我遇到很多错误......

ContentView 代码(允许我运行此代码的 hacking swift 代码除外)

struct Quote: Codable {
        
        var text: String
        var author: String
        
        var shareMessage: String {
            return "\"\(text)\" - \(author)"
        }
    }
        
        struct PytaniaNormalne : View {
            @State private var quote : Quote?
            var body: some View {
                VStack {
                    if let quote = quote {
                        VStack {
                            VStack {
                                Text(quote.text)
                                    .font(.system(.title3))
                                    .foregroundColor(.white)
                                Text(quote.author)
                                    .font(.system(.title3))
                                    .foregroundColor(.white)
                            }
                        }.frame(width: 240)
                            .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
                    }
                }.onAppear {
                    let quotes = Bundle.main.decode([Quote].self, from: "quotes.json")
                    quote = quotes.randomElement()
                    
                }
            }
        }

文件

[
    {
        "text": "Pytanie 1",
        "author": "tekst"
    },
    {
        "text": "Pytanie 2",
        "author": "tekst"
    },
    {
        "text": "Pytanie 3",
        "author": "teskt"
    },
    {
        "text": "Pytanie 4",
        "author": "tekst"
    },
    {
        "text": "Pytanie 5",
        "author": "author"
    },
    {
        "text": "Pytanie 6",
        "author": "author"
    },
    {
        "text": "Pytanie 7",
        "author": "author"
    }
]

【问题讨论】:

  • “我曾尝试通过函数创建它,但每次我都会遇到很多错误......”你能展示你的尝试吗?您可能知道如何制作Button,而在操作中只需要您的两条线let quotes = ...quote =

标签: ios xcode swiftui


【解决方案1】:

如您所见,我在authorText 下方添加了按钮。每次点击它,setRandomQuote 将被调用,为您的@State 变量设置一个随机引用,随后将重新加载视图。

struct PytaniaNormalne : View {
    @State private var quote : Quote?
    var body: some View {
        VStack {
            if let quote = quote {
                VStack {
                    VStack {
                        Text(quote.text)
                            .font(.system(.title3))
                            .foregroundColor(.white)
                        Text(quote.author)
                            .font(.system(.title3))
                            .foregroundColor(.white)
                        
                        Button(action: setRandomQuote) {
                            Text("Refresh")
                                .font(.system(.title3))
                                .foregroundColor(.white)
                                .padding()
                                .background {
                                    RoundedRectangle(cornerRadius: 8)
                                        .foregroundColor(.blue)
                                }
                        }

                    }
                }.frame(width: 240)
                    .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
            } else {
                Text("click the button")
                   .font(.system(.title3))
                   .foregroundColor(.white)
            }
        }
    }
    
    private func setRandomQuote() {
        let quotes = Bundle.main.decode([Quote].self, from: "quotes.json")
        quote = quotes.randomElement()
    }
}

【讨论】:

  • 它完美地工作。非常感谢!我尝试做完全相同的事情,但没有在 func 之前添加“私有”。我是一个新手,仍然不了解一些基础知识。谢谢你的帮助
  • private 关键字不会影响该程序的结果,只会影响函数的访问级别。这意味着,如果您确实将其标记为 private,那么拥有它的 struct 和只有 struct 才能看到并使用它。
  • 你好,我有一个问题。进入此视图时是否可以制作“欢迎文字”?现在,当用户进入此视图时,它会显示随机文本。我想制作欢迎文字,例如“单击按钮”。
  • 我在答案中添加了else。这就是它的工作原理;只要quote 属性为nil,就会激活else 语句。我还删除了onAppear 修饰符,以便在您按下按钮之前没有quote
猜你喜欢
  • 2012-09-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
  • 2021-12-10
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多