如何清除视图框

How to Clear a View Frame

我在视图中有一个重置按钮,当点击它时会出现一个警告,询问“你确定吗?”带有点击确定或取消的选项。如果用户点击确定,我希望按钮消失并显示“所有数据已被删除”。我尝试放置代码以在警报下显示最后一条消息,但我不断收到有关未使用代码的警告,因此将其放置在结构的较低位置。目前,当点击确定时,警报消失,代码将按钮推到视图顶部,并在下方居中显示完成消息。我猜我需要删除按钮框架,但不确定该怎么做。

@EnvironmentObject var userData: UserData
@State private var resetSys: Bool = false
@State private var okTapped: Bool = false

var body: some View {
        ZStack {  
            Button(action: {
                self.resetSys.toggle()
                
            }) {
                RoundedRectangle(cornerRadius: 25)
                .fill(Color.green)
                .frame(width: 165, height: 85)

                Text("Reset Data?")
                    
            }.disabled(okTapped)
        }
        .navigationBarTitle("Reset Trip Data", displayMode: .inline)
        
        .alert(isPresented: $resetSys) {
            
            Alert(title: Text("Are you sure?"), primaryButton: .destructive (Text("OK")) {
                okTapped = true
                self.userData.resetSysData()
            },
            secondaryButton: .cancel()
            )
        }
        
        if okTapped == true {
        
            ZStack {
                Color.white
                Text("All Data has been Deleted")
        }
    }
}

}

我不确定,也许你需要这样:

@EnvironmentObject var userData: UserData
@State private var resetSys: Bool = false
@State private var okTapped: Bool = false

var body: some View {
        ZStack {  
            if !self.okTapped {
                Button(action: {
                    self.resetSys.toggle()
                    
                }) {
                    RoundedRectangle(cornerRadius: 25)
                        .fill(Color.green)
                        .frame(width: 165, height: 85)
                    
                    Text("Reset Data?")
                    
                }.disabled(okTapped)
            } else {
                Color.white
                Text("All Data has been Deleted")
            }
        }
        .navigationBarTitle("Reset Trip Data", displayMode: .inline)
        .alert(isPresented: $resetSys) {
            
            Alert(title: Text("Are you sure?"), primaryButton: .destructive (Text("OK")) {
                okTapped = true
                self.userData.resetSysData()
            },
            secondaryButton: .cancel()
            )
        }
    }
}