How to add new content SwiftUI Intermediate List (2020 Tutorial)

Function Description

  • How to use List cycle through array content
  • .self as the id of
  • How to Update List Content
  • TextField Basics

Code

import SwiftUI

struct ListAddItemView: View {
    @State var products = ["手机","电脑","水杯"]
    @State var pName:String = ""
    var body: some View {
        VStack{
            TextField("新商品:",text: self.$pName)
            Button(action:{
                print("hello")
                if (self.pName != "")
                {
                    self.products.append(self.pName)
                    self.pName = ""
                    
                }
                
            }){
                Text("添加一个商品")
            }
            
            List(products,id:\.self){ item in
                Text(item)
                
            }
            
        }
        
        
    }
}

effect


More attention SwiftUI tutorials and code columns

Published 637 original articles · won praise 4 · views 50000 +

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/104079666