小编aye*_*lvs的帖子

SwiftUI:无法将“Bool”类型的值转换为预期的参数类型“Binding<Bool>”

为 SwiftUI 创建一个基于文本的游戏

想知道为什么我不能在字符 ForEach 循环中访问 isSelected?

收到此错误:无法在此行将“Bool”类型的值转换为预期的参数类型“Binding”

Toggle(isOn: character.isSelected){

变量对象声明: @Binding var characters: [Character]

代码在这里:

        VStack {

            ForEach(characters) { character in

                HStack{

                    VStack(alignment:.leading) {
                        Text("\(character.name)")
                            .fontWeight(.bold)

                        Text("\(character.description)")
                        .lineLimit(10)

                    }

                    Spacer()

                    Toggle(isOn: character.isSelected){
                        Text("a")
                    }.labelsHidden()
Run Code Online (Sandbox Code Playgroud)

swiftui

18
推荐指数
4
解决办法
1万
查看次数

如何通过函数/方法调用SwiftUI模态表?

我以前尝试过将.sheet附加在列表上,并且可以正常工作。但是出于我的项目目的,我想使用一个函数来调用模式警报。

import SwiftUI

struct Battlefield : View {

    @State var isModalInputPresented: Bool = false
    @State var inputTitle: String = ""
    @State var inputMessage: String = ""
    @State var inputValue: Int = 0

    private func summonModalInput(title: String, message: String, input: Int) {
        self.isModalInputPresented.toggle()
        self.inputTitle = title
        self.inputMessage = message
        self.inputValue = input
        sheet(isPresented: $isModalInputPresented, content: { InputView(inputTitle: self.$inputTitle, inputMessage: self.$inputMessage, inputValue: self.$inputValue, isPresented: self.$isModalInputPresented)})
    }

    var body: some View {
        summonModalInput(title: "foo", message: "bar", input: 0)
    }
}
Run Code Online (Sandbox Code Playgroud)

swift swiftui

1
推荐指数
1
解决办法
253
查看次数

标签 统计

swiftui ×2

swift ×1