如何在Swift中为文本添加制表符

x0fgdtte  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(212)

How can I make these be in their own tabs so users can tap and write their own things inside
这是我现在的代码。我是新的IOS开发,想知道如何添加的能力,以轻敲的名称,并能够写在他们单独。
结构LandingView:视图{

@State private var newLandingName: String = ""
@State private var isAddingLanding = false
@State private var landings: [String] = []

var body: some View {
    NavigationView {
        ZStack {
            Color.blue.ignoresSafeArea()
            Circle()
                .scale(1.7)
                .foregroundColor(.yellow.opacity(0.60))
            Circle()
                .scale(1.3)
                .foregroundColor(.blue.opacity(0.50))
            
            VStack {
                                HStack {
                                    Text("Add Your Travels")
                                        .bold()
                                    Spacer()
                                    Button(action: {
                                        isAddingLanding = true
                                    }) {
                                        Image(systemName: "plus")
                                            .resizable()
                                            .frame(width: 20, height: 20)
                                            .foregroundColor(.white)
                                    }
                                }
                                .padding(.horizontal)
                                
                                List(landings, id: \.self) { landing in
                                    Text(landing).bold()
                                        .foregroundColor(.white)
                                        .listRowBackground(Color.clear)
                                        .listRowSeparatorTint(.blue)
                                }
                                .listStyle(.plain)
                            }
                            
                        }
                        .navigationBarTitle(Text("Landings!"))
                        
                        .fullScreenCover(isPresented: $isAddingLanding, onDismiss: {}) {
                            VStack {
                                TextField("Where did you go? \(newLandingName)", text: $newLandingName)
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .padding()
                                Button("Add") {
                                    if !newLandingName.isEmpty {
                                        landings.append(newLandingName)
                                        newLandingName = ""
                                    }
                                    isAddingLanding = false
                                }
                                Color.blue.ignoresSafeArea()
                            }
                        }
                        
                    }
                }

}
我试着添加我在网上找到的片段,但似乎没有什么工作。有人能帮我吗

2guxujil

2guxujil1#

我想我明白你的意思了,下面是更新添加到列表中的值的代码,当然这段代码可以增强以避免样板代码:

@State private var oldLandingName: String = ""
@State private var newLandingName: String = ""
@State private var isAddingLanding = false
@State private var landings: [String] = []
@State private var isEditing = false

var body: some View {
    NavigationView {
        ZStack {
            Color.blue.ignoresSafeArea()
            Circle()
                .scale(1.7)
                .foregroundColor(.yellow.opacity(0.60))
            Circle()
                .scale(1.3)
                .foregroundColor(.blue.opacity(0.50))
            
            VStack {
                HStack {
                    Text("Add Your Travels")
                        .bold()
                    Spacer()
                    Button(action: {
                        isAddingLanding = true
                    }) {
                        Image(systemName: "plus")
                            .resizable()
                            .frame(width: 20, height: 20)
                            .foregroundColor(.white)
                    }
                }
                .padding(.horizontal)
                
                List(landings, id: \.self) { landing in
                    Text(landing).bold()
                        .foregroundColor(.white)
                        .listRowBackground(Color.clear)
                        .listRowSeparatorTint(.blue)
                        .onTapGesture {
                            self.isEditing = true
                            self.oldLandingName = landing
                            self.newLandingName = landing
                        }
                }
                .listStyle(.plain)
            }
            
        }
        .navigationBarTitle(Text("Landings!"))
        
        .fullScreenCover(isPresented: $isAddingLanding, onDismiss: {}) {
            VStack {
                TextField("Where did you go? \(newLandingName)", text: $newLandingName)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()
                Button("Add") {
                    if !newLandingName.isEmpty {
                        landings.append(newLandingName)
                        newLandingName = ""
                    }
                    isAddingLanding = false
                }
                Color.blue.ignoresSafeArea()
            }
        }
        .fullScreenCover(isPresented: $isEditing, onDismiss: {}) {
            VStack {
                TextField("", text: $newLandingName)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()
                Button("Update") {
                    if !newLandingName.isEmpty {
                        if let idx = landings.firstIndex(where: { $0 == $oldLandingName.wrappedValue }) {
                            self.landings[idx] = newLandingName
                        }
                    }
                    isEditing = false
                }
                Color.blue.ignoresSafeArea()
            }
        }
        
    }
}

我希望这对你有帮助,只是试着理解SwiftUI状态的使用,因为我们有@State和@Binding来处理结构体,还有@StateObject和@Published来处理类。不过,还有其他属性 Package 器可以使用。

相关问题