[一、基础控件]10使用SecureField密文输入框接收用户的密码
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/16587205.html
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
本文演示SecureField视图的使用。
【SceneDelegate.swift】:ContextView添加了一个属性,所以需要修改程序入口处的代码
1 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 2 // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 3 // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 4 // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 5 6 // Use a UIHostingController as window root view controller 7 if let windowScene = scene as? UIWindowScene { 8 let window = UIWindow(windowScene: windowScene) 9 //由于给ContextView添加了一个属性,所以需要修改程序入口处的代码,设置password的初始值为空。 10 window.rootViewController = UIHostingController(rootView: ContentView(password: "")) 11 self.window = window 12 window.makeKeyAndVisible() 13 } 14 }
【ContentView.swift】
1 import SwiftUI 2 3 struct ContentView : View { 4 //要使用SecureField视图, 5 //首先给当前的结构体添加一个属性。 6 //该属性用于存储用户输入的密码,并在属性的左侧添加一个@State属性代理标记, 7 //从而使属性和界面元素进行绑定。 8 @State var password : String 9 10 var body: some View { 11 12 VStack{ 13 //修改文本视图,以实时显示属性的值。 14 Text("Your password is \(password)!") 15 16 //添加一个密文输入框,并设置指定的占位符。 17 //同时设置它的text的值为password属性的值, 18 //并通过$符号和password属性进行绑定包装。 19 SecureField("Your password", text: $password) { 20 //在用户完成密码的输入之后,在尾随的闭包语句中,打印用户输入的密码信息。 21 print("Your password is \(self.password)!") 22 } 23 //设置输入框的边框样式为圆角样式 24 .textFieldStyle(RoundedBorderTextFieldStyle()) 25 } 26 //设置VStack视图的内边距,使SecureField与屏幕两侧保持一定的距离 27 .padding() 28 } 29 } 30 31 #if DEBUG 32 struct ContentView_Previews : PreviewProvider { 33 34 static var previews: some View { 35 //然后同样对PreviewProvider的代码进行一些修改。 36 ContentView(password: "") 37 } 38 } 39 #endif
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过