【问题标题】:How to select an item from a search file and place in textfield in another file如何从搜索文件中选择一个项目并将其放置在另一个文件的文本字段中
【发布时间】:2023-02-02 21:21:44
【问题描述】:

使用 SwiftUI - Xcode 14.2 - iOS 16.0

我已经尝试了不同的搜索教程来为我的项目创建一个搜索文件,但我无法找到如何在搜索文件中选择项目并将所选项目放置在另一个文件的文本字段中。我在这个网站上搜索了其他帖子,我尝试通过谷歌、YouTube 等搜索...

在文件 1 中,我有一个文本字段,它有一个提示“开始输入”,选择后,它会将您定向到搜索文件以选择您想要的项目,因此可以将其放在提示的位置。

文件 1(需要文本字段来粘贴所选项目):

VStack {
     NavigationLink(destination: NameSearch()) {
         TextField("Name", text: .constant(""), prompt: Text("   Start typing  ")
              .foregroundColor(.blue))
              .multilineTextAlignment(.leading)
              .padding()
     }
}

单击“开始输入”提示后,它会导航到 NameSearch.swift 文件,如下所示。

名称搜索.swift:

import SwiftUI

struct NameSearch: View {
    
    let name = [
        "Jane", "George", "Sam", "Henry", "Sally", "Liz", "John"
    ]
    
    @State private var searchText = ""
    
    var body: some View {
        
        NavigationStack {
            VStack {
                // Search view
                SearchBarView(searchText: $searchText)
                
                List {
                    // Filtered list of names
                    ForEach(name.filter{$0.hasPrefix(searchText) || searchText == ""}, id:\.self) {
                        searchText in Text(searchText)
                    }
                }
                .navigationBarTitle(Text("Search Name"))
                .resignKeyboardOnDragGesture()
            }
        }
    }
}


struct NameSearch_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            NameSearch()
                .environment(\.colorScheme, .light)
            NameSearch()
                .environment(\.colorScheme, .dark)
        }
    }
}

extension UIApplication {
    func endEditing(_ force: Bool) {
        self.windows
            .filter{$0.isKeyWindow}
            .first?
            .endEditing(force)
    }
}

struct ResignKeyboardOnDragGesture: ViewModifier {
    var gesture = DragGesture().onChanged{_ in
        UIApplication.shared.endEditing(true)
    }
    func body(content: Content) -> some View {
        content.gesture(gesture)
    }
}

extension View {
    func resignKeyboardOnDragGesture() -> some View {
        modifier(ResignKeyboardOnDragGesture())
    }
}


struct SearchBarView: View {
    
    @Binding var searchText: String
    @State private var showCancelButton: Bool = false
    var onCommit: () ->Void = {print("onCommit")}
    
    var body: some View {
        HStack {
            HStack {
                Image(systemName: "magnifyingglass")
                
                // Search text field
                ZStack (alignment: .leading) {
                    if searchText.isEmpty { // Separate text for placeholder to give it the proper color
                        Text("Search")
                    }
                    TextField("", text: $searchText, onEditingChanged: { isEditing in
                        self.showCancelButton = true
                    }, onCommit: onCommit).foregroundColor(.primary)
                }
                // Clear button
                Button(action: {
                    self.searchText = ""
                }) {
                    Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
                }
            }
            .padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
            .foregroundColor(.secondary) // For magnifying glass and placeholder test
            .background(Color(.tertiarySystemFill))
            .cornerRadius(10.0)
            
            if showCancelButton  {
                // Cancel button
                Button("Cancel") {
                    UIApplication.shared.endEditing(true) // this must be placed before the other commands here
                    self.searchText = ""
                    self.showCancelButton = false
                }
                .foregroundColor(Color(.systemBlue))
            }
        }
        .padding(.horizontal)
        .navigationBarHidden(showCancelButton)
    }
}

问题 1:如何隐藏列表中显示的所有名称,以便我只看到搜索栏和取消按钮以及一个空列表?

问题 2:一旦我输入我要查找的名称,它就会弹出,我想选择名称 - 我该怎么做?

  • 一旦我在搜索栏中输入名称,它就会出现在空列表中
  • 我选择那个名字
  • 然后它带我回到文件 1
  • 将“开始输入”提示替换为我刚刚在搜索文件中选择的名称。

问题 3:我注意到在搜索文件中,我收到了一条包含以下代码的警告。我该如何解决?

extension UIApplication {
    func endEditing(_ force: Bool) {
        self.windows
            .filter{$0.isKeyWindow}
            .first?
            .endEditing(force)
    }
}

出现的警告是:

'windows' 在 iOS 15.0 中被弃用:在 a 上使用 UIWindowScene.windows 相关的窗口场景代替

【问题讨论】:

  • 专业提示:样板注释说“我已经搜索过,老实说,我发誓我的宠物蛇的生命”是无用的,除非它们是详细的。无需提供指向 youtube.com、google.com 等的链接 - 读者知道这些网站,不需要提醒它们。如果你想展示你的研究——通常是个好主意——那就尽可能地展示你的研究,以及你尝试的代码。

标签: search swiftui ios16


【解决方案1】:

首先,感谢您提供代码的工作示例。

在为 iOS 15+ 构建时,您可能应该使用 .searchable 修饰符而不是自己滚动。

介绍此功能的 2021 WWDC 视频在这里https://developer.apple.com/wwdc21/10176

2022 年的一些新功能:https://developer.apple.com/wwdc22/10052

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多