「SwiftUI」Alert弹窗

IOS Alert弹窗

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

在这里插入图片描述

相应代码

@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默认参数

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

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

在这里插入图片描述

相应代码

@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指令

在这里插入图片描述

相应代码

 @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开发者文档相应网址:

https://developer.apple.com/documentation/swiftui/alert/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡酒交魂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值