首页 > 解决方案 > 如何创建仅在使用 SwiftUI 打开应用程序时出现的警报?

问题描述

我是应用程序开发的新手,最近创建了一个名为靶心的游戏。

我想在首次打开应用程序时显示一条警报,向用户提供有关他们将要玩的游戏的说明。

我在没有按钮的情况下在 swiftUI 中创建此警报时遇到问题,想知道是否有人可以帮助我解决此问题。

PS - 我正在使用 Xcode 11

谢谢

标签: iosswiftswiftuialert

解决方案


我不是 SwiftUI 专家,但这对我有用:

import SwiftUI

struct ContentView: View {

    // In the future, you can use this var to control whether or not to show an alert based on some other condition.
    @State var alertShouldBeShown = true

    var body: some View {
        // attach the alert to the last view in your view hierarchy
        Text("Hello, World!").alert(isPresented: $alertShouldBeShown, content: {

            Alert(title: Text("Alert:"),
                  message: Text("This is the alert. :)"),
                  dismissButton: Alert.Button.default(
                    Text("OK"), action: {

                        //

                  }
                )
            )
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐阅读