如何在 SwiftUI 中自动填充文本字段?

Posted

技术标签:

【中文标题】如何在 SwiftUI 中自动填充文本字段?【英文标题】:How to auto fill a text field in SwiftUI? 【发布时间】:2021-02-02 15:34:38 【问题描述】:

我正在制作一个在 SwiftUI 中使用表单的应用,我想使用所述表单编辑本地存储的对象。

在这种情况下,我有一个蛋糕和一个饼干要存储在内存中,我有两个结构,一个用于蛋糕,一个用于饼干。

当用户选择他/她要编辑的蛋糕或饼干时,表单字段应自动填充实际存储的可编辑蛋糕或饼干的属性。

结构是:

import Foundation

struct Cake 
    var name : String
    var style : String
    var decorations : String


struct Cookie
    var name : String
    var style : String

我用于视图的代码是:

import SwiftUI

struct EditingForm: View 
    @State var cake : Cake = Cake(name: "Caprese", style: "Chocolate cake made with almonds flour", decorations: "Powdered Sugar")
    
    @State var cookie : Cookie = Cookie(name: "Chocolate Chip", style: "Cookie made with chocolate chips")
    
    @State var group : String = "Cake"
    @State var nameCake : String = ""
    @State var nameCookie : String = ""
    @State var styleCake : String = ""
    @State var cookiesChocolateChipsType : String = ""
    
    var body: some View 
        ZStack
            Form
                Section(header: Text("Sweet Details")
                )
                    Menu("Select the sweet you want to edit") 
                        Button(action: 
                            group = "Cake"
                        ) 
                            Text("Cake")
                        .foregroundColor(.black)
                        
                        Button(action: 
                            group = "Cookie"
                        ) 
                            Text("Cookie")
                        
                    
                
                
                Section(header: Text("Decorations")
                )
                    if(group == "Cake")
                        TextField("Name of the cake", text: $nameCake)
                        TextField("Style of the cake", text: $styleCake)
                     else if (group == "Cookie")
                        TextField("Name of the cookie", text: $nameCookie)
                        TextField("Type of chocolate chips for the cookie", text: $cookiesChocolateChipsType)
                    
                
            
        
    
    

【问题讨论】:

【参考方案1】:

您可以简单地使用.onAppear() 修饰符。这意味着,当 View 出现时,您的 @State var 会得到更新。

在您的示例中,代码如下所示:

import SwiftUI

struct EditingForm: View 
    @State var cake : Cake = Cake(name: "Caprese", style: "Chocolate cake made with almonds flour", decorations: "Powdered Sugar")
    
    @State var cookie : Cookie = Cookie(name: "Chocolate Chip", style: "Cookie made with chocolate chips")
    
    @State var group : String = "Cake"
    @State var nameCake : String = ""
    @State var nameCookie : String = ""
    @State var styleCake : String = ""
    @State var cookiesChocolateChipsType : String = ""
    
    var body: some View 
        ZStack
            Form
                Section(header: Text("Sweet Details")
                )
                    Menu("Select the sweet you want to edit") 
                        Button(action: 
                            group = "Cake"
                        ) 
                            Text("Cake")
                        .foregroundColor(.black)
                        
                        Button(action: 
                            group = "Cookie"
                        ) 
                            Text("Cookie")
                        
                    
                
                
                Section(header: Text("Decorations")
                )
                    if(group == "Cake")
                        TextField("Name of the cake", text: $nameCake).onAppear() 
                          self.nameCake = self.cake.name
                        
                        TextField("Style of the cake", text: $styleCake).onAppear() 
                          self.styleCake = self.cake.style
                        
                     else if (group == "Cookie")
                        TextField("Name of the cookie", text: $nameCookie).onAppear() 
                          self.nameCookie = self.cookie.name
                        
                        TextField("Type of chocolate chips for the cookie", text: $cookiesChocolateChipsType).onAppear() 
                          self.cookiesChocolateChipsType = self.cookie.style
                        
                    
                
            
        
    
    

希望这将有助于解决您的问题。

【讨论】:

以上是关于如何在 SwiftUI 中自动填充文本字段?的主要内容,如果未能解决你的问题,请参考以下文章

Magento 2 如何使用淘汰赛 JS 自动填充文本字段

Flutter 中的自动填充文本字段

SwiftUI 中的名称字段

如何使用@EnvironmentObject 在 SwiftUI 中自动填充和更新列表?

在 SwiftUI 中向 TextField/SecureField 添加图像,向占位符文本添加填充

如何自动完成自动填充文本框