ios SwiftUI:我如何从一个视图中更改索引,并在另一个视图中显示正确的文本?

ewm0tg9j  于 2022-12-24  发布在  iOS
关注(0)|答案(1)|浏览(174)

我不明白为什么我的DetailView显示相同的文本。(gif如下)
实际上,我在ContentView中实现了一个方法selectTheme,它应该在DetailView出现时根据行的索引更改selectedIndex,但它似乎没有考虑到我的ViewModel中的var selectedIndex仍然为0。
你知道为什么吗?我真的不明白出了什么问题。
谢谢你。

struct ContentView: View {
    var vm:ViewModel
    var body: some View {
        NavigationView{
            List{
                ForEach(vm.sentences, id:\.self) { indexNumb in
                    NavigationLink(destination: DetailView(vm: vm).onAppear(perform: {
                        vm.selectTheme(sentence: indexNumb)
                        print(vm.groupedItems)
                        print("Array :\(vm.sylbArray)")
                        
                    })) {
                        Text(String(indexNumb))
                        
                    }
                }
                
            }
        }
    }
}
struct DetailView: View {
    var vm:ViewModel
    
    var body: some View {
        
        ForEach(vm.groupedItems,id:\.self) { subItems in
            HStack{
                
                ForEach(subItems,id:\.self) { word in
                    
                    Button(action: {
                        print(vm.groupedItems)
                        print(vm.selectedIndex)
                        
                    }, label: {
                        Text(word)
                    })
                    
                }
            }
        }
    }
}
class ViewModel{
    
    @Published var sylbArray: [String] = []
    var groupedItems: [[String]] = []
    
    init() {
        appendArray(string: String(sentences[selectedIndex]))
        groupedItems = [sylbArray]
    }
    
    var sentences = [13,4]
    
    func appendArray(string: String) {
        sylbArray.append(string)
    }
    
//  remains  0 : why ? 
    var selectedIndex = 0
    
    func selectTheme(sentence: Int) {
        if let index = sentences.firstIndex(of: sentence) {
            selectedIndex = index
        }
    }
}

czq61nw1

czq61nw11#

您的viewModel必须符合类协议“ObservableObject”

class ViewModel: ObservableObject {
    var sentences = [13, 4]
    @Published var selectedSentens: Int?
}

下面是您要执行操作的示例。

struct ContentView: View {
    @StateObject var vm: ViewModel = .init()
    var body: some View {
        NavigationSplitView {
            List(vm.items, selection: $vm.selectedItem) { item in
                Text(item.name)
                    .tag(item)
            }
        } detail: {
            if let item = vm.selectedItem{
                DetailView(for: item)
            }
        }
        .navigationSplitViewStyle(.balanced)
    }
}

项目对象:

struct Item: Hashable, Identifiable {
    var name: String
    var subItems: [String]
    var id = UUID()
}

视图模型类

class ViewModel: ObservableObject {
    var items: [Item] = [
        Item(name: "Item 1", subItems: [ "sub 11", "sub 12" ] ),
        Item(name: "Item 2", subItems: [ "sub 21", "sub 22" ] )
    ]
    @Published var selectedItem: Item?
}

详细视图:

struct DetailView: View {
    var item: Item
    init(for item: Item) {
        self.item = item
    }
    var body: some View {
        List(item.subItems, id: \.self){ subItem in
            Text(subItem)
        }
    }
}

相关问题