【发布时间】:2020-05-06 08:59:33
【问题描述】:
我想使用我的 AlamoFire .post 请求方法中的数据在我的 SwiftUI 应用程序中填充一些 TextFields,其中包含预先填充的选项。
这可能吗?
干杯, 奥利弗
【问题讨论】:
-
你有没有设法解决这个问题,因为我有同样的问题
标签: drop-down-menu alamofire swiftui textfield populate
我想使用我的 AlamoFire .post 请求方法中的数据在我的 SwiftUI 应用程序中填充一些 TextFields,其中包含预先填充的选项。
这可能吗?
干杯, 奥利弗
【问题讨论】:
标签: drop-down-menu alamofire swiftui textfield populate
我想我明白你在问什么,答案是肯定的。最简单的方法是在默认的 ContentView 之外创建一个专用的 TextField 视图。
struct MyAlamoFireText: View {
@State var someText = ""
var body: some View {
TestField("Default prompt", text: $someText)
}
}
struct ContextView: View {
var body: some View {
MyAlamoFireText(someText: "Initial value of text")
}
}
基本上,您需要的是一个间接级别,它将设置字段中最初显示的文本。如何获取需要传递到其中的数据取决于您的架构。
【讨论】:
如果这是您正在寻找的,那么这就是您实现这一目标的方法。
//
// SellerbuddyAutoCompleteView.swift
// iosApp
//
// Created by Vikas on 21/08/21.
// Copyright © 2021 orgName. All rights reserved.
//
import SwiftUI
struct SellerbuddyAutoCompleteView: View {
var errorMsg: String = ""
var isDisabled: Bool = false
@Binding var text: String
let hint : String
@Binding var isValid: Bool
@State var hasOptions : Bool
@State var options: [String]
var body: some View {
VStack(alignment: .leading) {
TextField(LocalizedStringKey(hint), text: $text)
.padding(.all, 12)
.overlay(
RoundedRectangle(cornerRadius: 6).stroke(getEditTextColor(isValid), lineWidth: 1)
).overlay(
ZStack{
if hasOptions {
Spacer().frame(height: 50)
VStack(alignment: .center) {
ForEach(options, id: \.self) {
Text($0).frame(maxWidth: .infinity).padding(8)
Divider()
}
}.frame(maxWidth: .infinity).background(RoundedRectangle(cornerRadius: 6).foregroundColor(.white).shadow(radius: 4))
}
}.offset(y: 50)
, alignment: .topLeading)
.overlay(
Image(systemName: "arrowtriangle.down.circle.fill").resizable().frame(width: 20, height: 20).padding().onTapGesture {
self.hasOptions = false
}
, alignment: .trailing)
Text(errorMsg)
.visibility(hidden: $isValid).padding(.leading, 4)
.foregroundColor(Color.red)
.offset(y: -5)
.frame(height: isValid ? 0: 10)
.animation(.easeInOut)
}.padding([.top, .leading, .trailing], 8).frame(height: 45)
}
func getEditTextColor(_ isValid: Bool) -> Color {
if !isValid {
return Color.red
}else if isDisabled {
return Color.gray
} else {
return Color.blue
}
}
}
struct SellerbuddyAutoCompleteView_Previews: PreviewProvider {
static var options = [String](arrayLiteral: "Afghanistan","America")
static var previews: some View {
SellerbuddyAutoCompleteView(errorMsg : "Some error has happened",text: Binding.constant("Search countries here"), hint: "signin_hint_email_id", isValid: Binding.constant(true), hasOptions: (true), options: options)
}
}
上面显示了自定义视图的代码,您需要从一些 swiftui 视图调用此视图以启用视图中提到的所有数据。请根据需要进行其他更改,例如 ui、颜色和字体等。
【讨论】: