SwiftUI之收货地址页面实现

15 篇文章 2 订阅
7 篇文章 4 订阅

效果

在这里插入图片描述
在这里插入图片描述

思路

表单列表可用Form实现

分成三段实现

  1. 填写表单
  2. 选择标签
  3. 提交按钮

实现代码

struct ContentView: View {
    // State属性包装器 属性就可以在struct中进行更改
    @State private var name = ""
    @State private var phone = ""
    @State private var address = ""
    @State private var tagIndex = 0
    @State private var showAlert = false
    
    let tags = ["家", "学校", "公司"]
    
    var confirmMsg: String {
        """
        收货人是:\(name)
        手机号: \(phone)
        详细地址: \(address)
        标签: \(tags[tagIndex])
        """
    }
    
    var invalidInput: Bool {
        name.isEmpty || phone.isEmpty || address.isEmpty
    }
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    HStack {
                        Text("收货人")
                        TextField("请填写收货人姓名", text: $name)
                    }
                    HStack {
                        Text("手机")
                        TextField("请填写收货人手机号", text: $phone)
                            .keyboardType(.numberPad)
                    }
                    HStack {
                        Text("详细地址")
                        TextField("请填写收货人地址", text: $address)
                    }
                }
                
                Section(header: Text("标签")) {
                    Picker("xx", selection: $tagIndex) {
                        ForEach(0..<tags.count, id: \.self) { index in
                            Text(self.tags[index])
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }
                
                Section {
                    Button("填好了") {
                        self.showAlert = true
                    }.disabled(invalidInput)
                    
                }
                
            }
            .navigationBarTitle("新增收货地址")
            .alert(isPresented: $showAlert) {
                Alert(title: Text("确认地址"),
                      message: Text(confirmMsg),
                      dismissButton: .default(Text("确认")))
            }
        }
        
    }
}

参考:
Lebus系列教程

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值