本文介绍了SwiftUI Button作为EditButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 EditButton 之类的按钮?

还是如何使用按钮从列表中激活编辑模式?

还是如何从 EditButton edit / done更改名称?

How I can use a button like EditButton?
Or how I can use a button to active edit mode from list?
Or how I can change the name from EditButton "edit" / "done"?

推荐答案

下面的实现将 EditButton 的功能替换为 Button

The implementation below replaces EditButton's functionality with a Button:

import SwiftUI

struct ContentView: View {

    @State var isEditing = false
    @State var selection = Set<String>()

    var names = ["Karl", "Hans", "Faustao"]

    var body: some View {
        NavigationView {
            VStack {
                List(names, id: \.self, selection: $selection) { name in
                    Text(name)
                }
                .navigationBarTitle("Names")
                .environment(\.editMode, .constant(self.isEditing ? EditMode.active : EditMode.inactive)).animation(Animation.spring())
                Button(action: {
                    self.isEditing.toggle()
                }) {
                    Text(isEditing ? "Done" : "Edit")
                        .frame(width: 80, height: 40)
                }
                .background(Color.yellow)
            }
            .padding(.bottom)
        }
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif



但是,通过这样做,选择处理需要由我们自己执行(这可能是问题,也可能不是问题)。

However, by doing so, selection handling needs to be implemented by our own (which may or may not be an issue).

目前,有关这一点的文档并不多:

Unfortunately there isn't much documentation around that at this point:https://developer.apple.com/documentation/swiftui/list/3367016-init

这篇关于SwiftUI Button作为EditButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:31