如何在 SwiftUI 中关闭模态 sheet 后推送到新屏幕?
How to push on new screen after dismiss model sheet in SwiftUI?
我必须像这样实现要在屏幕中创建登录屏幕用户可以输入手机号码并单击登录按钮模型sheet将打开顶部验证OTP 验证后 sheet 必须关闭并在仪表板上导航。
我在该内容视图上创建了一个内容视图,单击 打开模型 按钮,模型 sheet (sheetView) 将在单击 完成 按钮后打开 sheet 将关闭,而不是屏幕从 ContentView 导航到 DashboardView.
//内容视图
struct ContentView: View {
@State var isAction: Bool = false
@State var isDashboard: Bool = false
var body: some View {
NavigationView{
Button(action: {
self.isAction.toggle()
}) {
Text("Open Model")
}.sheet(isPresented: $isAction) {
sheetView(isDissmis: self.$isAction) { (isNavigateOnDashboard) in
print(isNavigateOnDashboard ?? false)
return self.isDashboard = isNavigateOnDashboard ?? false
}
}
NavigationLink(destination: DashboardView(), isActive: self.$isDashboard) {
EmptyView()
}
}
}
}
//sheet查看:-以sheet的形式呈现为modalPresentationStyle
。
struct sheetView: View {
@Binding var isDissmis: Bool
var isNavigateDashboard: ((Bool?)) -> Void?
var body: some View {
NavigationView{
Text("Here i have to verifiy Some details")
.navigationBarItems(trailing: doneButton)
}
}
var doneButton: some View {
Button(action: {
self.isDissmis = false
self.isNavigateDashboard(true)
}) {
Text("Done").bold()
}
}
}
//仪表板:- 关闭模型(sheet)后屏幕应在仪表板上导航。
struct DashboardView: View {
var body: some View {
TabView{
Text("Home").tabItem{
Image(systemName: "1.circle")
Text("Home")
}
Text("Activity").tabItem{
Image(systemName: "2.circle")
Text("Activity")
}
Text("profile").tabItem{
Image(systemName: "3.circle")
Text("profile")
}
}
}
}
有什么线索吗?
您可以通过这种方式更改 ContentView
body
,当您关闭 sheet 视图时,它将导航至 DashboardView
此外,您的 SheetView
中无需使用 clouser 回调
var body: some View {
NavigationView{
VStack {
Button(action: {
self.isAction.toggle()
}) {
Text("Open Model")
}.sheet(isPresented: $isAction, onDismiss: {
self.isDashboard.toggle()
}) {
sheetView(isDissmis: self.$isAction)
}
NavigationLink(destination: DashboardView(), isActive: $isDashboard) {
EmptyView()
}
}
}
}
我必须像这样实现要在屏幕中创建登录屏幕用户可以输入手机号码并单击登录按钮模型sheet将打开顶部验证OTP 验证后 sheet 必须关闭并在仪表板上导航。
我在该内容视图上创建了一个内容视图,单击 打开模型 按钮,模型 sheet (sheetView) 将在单击 完成 按钮后打开 sheet 将关闭,而不是屏幕从 ContentView 导航到 DashboardView.
//内容视图
struct ContentView: View {
@State var isAction: Bool = false
@State var isDashboard: Bool = false
var body: some View {
NavigationView{
Button(action: {
self.isAction.toggle()
}) {
Text("Open Model")
}.sheet(isPresented: $isAction) {
sheetView(isDissmis: self.$isAction) { (isNavigateOnDashboard) in
print(isNavigateOnDashboard ?? false)
return self.isDashboard = isNavigateOnDashboard ?? false
}
}
NavigationLink(destination: DashboardView(), isActive: self.$isDashboard) {
EmptyView()
}
}
}
}
//sheet查看:-以sheet的形式呈现为modalPresentationStyle
。
struct sheetView: View {
@Binding var isDissmis: Bool
var isNavigateDashboard: ((Bool?)) -> Void?
var body: some View {
NavigationView{
Text("Here i have to verifiy Some details")
.navigationBarItems(trailing: doneButton)
}
}
var doneButton: some View {
Button(action: {
self.isDissmis = false
self.isNavigateDashboard(true)
}) {
Text("Done").bold()
}
}
}
//仪表板:- 关闭模型(sheet)后屏幕应在仪表板上导航。
struct DashboardView: View {
var body: some View {
TabView{
Text("Home").tabItem{
Image(systemName: "1.circle")
Text("Home")
}
Text("Activity").tabItem{
Image(systemName: "2.circle")
Text("Activity")
}
Text("profile").tabItem{
Image(systemName: "3.circle")
Text("profile")
}
}
}
}
有什么线索吗?
您可以通过这种方式更改 ContentView
body
,当您关闭 sheet 视图时,它将导航至 DashboardView
此外,您的 SheetView
var body: some View {
NavigationView{
VStack {
Button(action: {
self.isAction.toggle()
}) {
Text("Open Model")
}.sheet(isPresented: $isAction, onDismiss: {
self.isDashboard.toggle()
}) {
sheetView(isDissmis: self.$isAction)
}
NavigationLink(destination: DashboardView(), isActive: $isDashboard) {
EmptyView()
}
}
}
}