「SwiftUI」Alert弹窗

IOS Alert弹窗

说明:gif制作会有重影,但代码实际并不会出现此问题,请大家谅解

相应代码

swift 复制代码
@State var isShowAlert:Bool = false
    var body: some View {
        VStack{
            Button {
                self.isShowAlert.toggle()
            } label: {
                Text("ShowAlert")
            }
        }
        .alert(isPresented: self.$isShowAlert) {
            Alert(title: Text("Title"), message: Text("message"))
        }
        
    }
通过使用视图修饰符来创建一个Alert弹窗,该弹窗需要绑定一个Binding类型,当Binding类型为true时,弹窗出现。上面代码设定一个isShowAlert的Binding监听变量,当摁下摁钮时,isShowAlert为true,弹窗出现

Alert默认参数

swift 复制代码
 Alert(title:Text,message:Text?,dismissButton:Alert.Button?) -> Alert
  • Title:弹窗标题
  • message:弹窗内容
  • dismissButton:可能为空的Alert.Button
    ps:message和dismissButton都可以选择使用
    当dismissButton为nil时,默认为"OK"的摁钮

有一个摁钮,并有相应action执行时

相应代码

swift 复制代码
@State var isShowAlert:Bool = false
    var body: some View {
        VStack{
            Button {
                self.isShowAlert.toggle()
            } label: {
                Text("ShowAlert")
            }
        }
.alert(isPresented: self.$isShowAlert) {
            Alert(title: Text("Title"), message: Text("message"), dismissButton:
                        .default(Text("关闭"), action: {
                print("dismissButton Action") //code
            }))
        }
 }

在点击"关闭"按钮时,触发action内容代码,实现print()

Button类型:

  • .default :具有默认样式的警报按钮

  • .cancel :

    • .cancel((() -> Void)?) -> Alert.Button :使用系统提供的标签创建一个指示取消的警报按钮
    • cancel(Text,action:(() -> Void )?) -> Alert.Button : 使用自定义标签创建一个指示取消的警报按钮
  • destructive : 具有指示破坏性操作的样式的警报按钮

两个摁钮,实现各自不同的action指令

相应代码

swift 复制代码
 @State var isShowAlert:Bool = false
    var body: some View {
        VStack{
            Button {
                self.isShowAlert.toggle()
            } label: {
                Text("ShowAlert")
            }
        }
        .alert(isPresented: self.$isShowAlert) {
            Alert(title: Text("Title"), message: Text("message"),
                  primaryButton: .default(
                      Text("Try Again"),
                      action: {
                          print("Try Again")
                      }
                  ),
                  secondaryButton: .destructive(
                      Text("Delete"),
                      action: {
                          print("Delete")
                      }
                  ))
        }
   }

需要添加primaryButton和secondaryButton(均为Alert Button类型),使用方法于dismissButton相同

提醒:该方法在Ios13之后已经弃用,大家各自使用时请注意

Apple开发者文档相应网址:

developer.apple.com/documentati...

相关推荐
Wenzar_1 天前
# 发散创新:SwiftUI 中状态管理的深度实践与重构艺术 在 SwiftUI 的世界里,**状态驱动 UI 是核心哲学**。但随
java·python·ui·重构·swiftui
大熊猫侯佩2 天前
GeometryReader 生存指南(下集):与恶魔共舞——陷阱、禁忌与最终救赎
swiftui·performance·layout·frame·stack·geometryreader·preferencekey
大熊猫侯佩3 天前
别被系统绑架:SwiftUI List 替换背后的底层逻辑
swiftui·swift·apple
东坡肘子4 天前
从 OpenSwiftUI 到 DanceUI:换个方式 Dive SwiftUI -- 肘子的 Swift 周报 #132
人工智能·swiftui·swift
用户79457223954135 天前
【SwiftyJSON】拯救你的 as? [String: Any]——链式 JSON 访问的正确姿势
swiftui·objective-c·swift
用户79457223954135 天前
【Moya】为什么你的 Alamofire 代码需要再封装一层?
swiftui·objective-c·swift
空中海6 天前
第二章:SwiftUI 视图基础
ios·swiftui·swift
择势6 天前
MVVM 本质解构 + RxSwift 与 Combine 深度对决与选型指南
swiftui·swift·rxswift
东坡肘子11 天前
被 Vibe 摧毁的版权壁垒,与开发者的新护城河 -- 肘子的 Swift 周报 #131
人工智能·swiftui·swift