Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使VStack可单击以在SwiftUI中显示警报_Ios_Swift_Swiftui_Swift5_Swift5.1 - Fatal编程技术网

Ios 如何使VStack可单击以在SwiftUI中显示警报

Ios 如何使VStack可单击以在SwiftUI中显示警报,ios,swift,swiftui,swift5,swift5.1,Ios,Swift,Swiftui,Swift5,Swift5.1,我有一个带有图像和文本的VStack,我需要使其可点击,以便向用户显示和提醒。我尝试了ontapsignature方法,但我能够打印语句,但警报没有显示 有没有其他方法可以得到解决方案 我已经添加了整个swift文件供您参考 代码 @State private var hasOneConnected: Bool = false @State private var showConnectionAlert = false private var connectionAlert: Alert {

我有一个带有图像和文本的VStack,我需要使其可点击,以便向用户显示和提醒。我尝试了ontapsignature方法,但我能够打印语句,但警报没有显示

有没有其他方法可以得到解决方案

我已经添加了整个swift文件供您参考

代码

@State private var hasOneConnected: Bool = false
@State private var showConnectionAlert = false

private var connectionAlert: Alert {
    Alert.init(title: Text("Oops!"), message: Text("There is an error."), dismissButton: .default(Text("OK")))
}
var body: some View {
    VStack(alignment: .center, spacing: 0) {

        // MARK: - Header
        VStack(alignment: .center, spacing: 0) {
            Spacer().frame(height: 55)
            HStack(alignment: .center, spacing: 25) {
                Spacer()
                Image("Logo")
                Spacer(minLength: 5)
                Text("Zone Control")
                    .foregroundColor(.white)
                Spacer()
            }
            Spacer()
        }
        .frame(height: 100, alignment: .center)
        .background(
            LinearGradient(
                gradient: Gradient(colors: [Color.Heat.primary, Color.Heat.primary.opacity(0.8), Color.Heat.primary.opacity(0.5)]),
                startPoint: .top, endPoint: .bottom
            )
        )
        // MARK: - Menu Bar
        HStack(alignment: .center, spacing: 10) {
            Spacer().frame(maxWidth: 20)

            HStack(alignment: .center, spacing: 10) {
                Image("batteryModule")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 8)
                Text("Pod Status")
                    .font(.caption)
                    .foregroundColor(.white)
            }

            Spacer()

            VStack(alignment: .center, spacing: 4) {
                Image(self.hasConnected ? "bluetoothConnected" : "bluetoothNotConnected")
                Text(self.hasConnected ? "Connected" : "Disconnected")
                    .font(.caption)
                    .foregroundColor(.white)
            }
            .frame(width: 80)

            VStack(alignment: .center, spacing: 4) {
                Image("batteryStatus")
                Text("\(self.batteryLevel)%")
                    .font(.caption)
                    .foregroundColor(.white)
            }
            .frame(width: 60)

            Spacer().frame(maxWidth: 10)
        }
        .padding()
        .background(Color.black)

    }

    .statusBar(hidden: true)
    .edgesIgnoringSafeArea(.all)
    .onAppear(perform: {
        UserDefaults.standard.set(true, forKey: "onboarding")
        self.update()
    })
        .onReceive(self.skiinBLE.objectWillChange) { _ in
            self.update()
    }

}

func update() {
    self.hasOneConnected = self.topLayer.cbPeripheral?.state != .disconnected
    self.batteryLevel = self.topLayer.moduleInformation?.batteryLevel.rawValue ?? 0

}

我已将您的代码简化为基本内容。您实际上不需要添加点击手势,您可以将整个元素(例如
VStack
)包装在
按钮中,并从那里触发警报。重要的一点是要记住,当用户点击
警报上的“确定”时,将
showConnectionAlert
设置回false。将其包装在
按钮中的副作用是,内部的所有内容都将以淡色呈现。这就是为什么我将
.foregroundCololor()
应用于
VStack
(对于某些图像,您可能还必须添加
.renderingMode(.original)
修改器):


要使空容器可点击(例如间隔器()),应使用
.contentShape(Rectangle())
修改器:

struct ContentView: View {

    @State private var showConnectionAlert = false

    var body: some View {
        Button(action: { self.showConnectionAlert = true }) {
            VStack(spacing: 4) {
                Image(systemName: "antenna.radiowaves.left.and.right")
                Text("Connected")
            }   .foregroundColor(.primary)
        }
            .alert(isPresented: $showConnectionAlert) {
                    Alert(title: Text("Nice"),
                          message: Text("The alert is showing!"),
                          dismissButton: Alert.Button.default(Text("OK"),
                                                              action: { self.showConnectionAlert = false }))
        }
    }
}
VStack(间距:4){
图像(系统名称:“天线、无线电波、左、右”)
文本(“已连接”)
垫片()
}
.contentShape(矩形())
.ontapsigne{
打印(“整个VStack现在可以点击了!”)
}

OnTap
对我来说很好。您将此视图放在何处以及如何调用它很重要。此警报是否适用于您@请包含定义视图的整个结构好吗?什么是“hasOneConnected”?它在文件中被引用,但未定义?我已通过剥离您的代码编写了一个答案-让我知道它是否有意义。但我想通过点击手势来完成。这并没有真正回答问题。谢谢。我在这里着陆也是因为这个原因,不是因为显示警报。