「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...

相关推荐
麦兜*1 天前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
大熊猫侯佩3 天前
「内力探查术」:用 Instruments 勘破 SwiftUI 卡顿迷局
swiftui·debug·xcode
HarderCoder3 天前
深入理解 SwiftUI 的 ViewBuilder:从隐式语法到自定义容器
swiftui·swift
东坡肘子3 天前
我差点失去了巴顿(我的狗狗) | 肘子的 Swift 周报 #098
swiftui·swift·apple
黄鹤的小姨子5 天前
SwiftUI 劝退实录:AI 都无能为力,你敢用吗?
swiftui
麦兜*6 天前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift
东坡肘子10 天前
苹果首次在中国永久关闭了一家 Apple Store | 肘子的 Swift 周报 #097
swiftui·swift·apple
大熊猫侯佩14 天前
WWDC 25 玻璃态星际联盟:SwiftUI 视图协同“防御协议”
swiftui·swift·wwdc
东坡肘子17 天前
Xcode 26 beta 4,要崩我们一起崩 | 肘子的 Swift 周报 #096
swiftui·swift·apple
吴Wu涛涛涛涛涛Tao18 天前
SwiftUI 打造 TikTok 风格的滑动短视频播放器
ios·swiftui