Ios 导航到上一个控制器SwiftUI
我目前处于特定瓶子详细视图的视图控制器中,并为用户提供一个操作表,使他们能够删除产品。因为他们可以删除此产品,所以一旦成功删除,我需要导航到上一个控制器。我怎样才能在斯威夫特做到这一点?这将在API请求成功的情况下完成Ios 导航到上一个控制器SwiftUI,ios,swift,swiftui,Ios,Swift,Swiftui,我目前处于特定瓶子详细视图的视图控制器中,并为用户提供一个操作表,使他们能够删除产品。因为他们可以删除此产品,所以一旦成功删除,我需要导航到上一个控制器。我怎样才能在斯威夫特做到这一点?这将在API请求成功的情况下完成 struct WishlistView: View { @State private var showingSheet = false @State private var show_modal: Bool = false let wishlist: Wi
struct WishlistView: View {
@State private var showingSheet = false
@State private var show_modal: Bool = false
let wishlist: Wishlist
var body: some View {
Text("Hello, Marketplace!")
.navigationBarTitle(wishlist.name)
.navigationBarItems(trailing:
HStack {
Button(action: {
showingSheet = true
}) {
Image(systemName: "ellipsis.circle.fill")
}
.actionSheet(isPresented: $showingSheet) {
ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
.default(Text("Delete Wishlist"), action: {
destroyWishlist()
}),
.default(Text("Green")),
.default(Text("Blue")),
.cancel()
])
}
.foregroundColor(Color("Rye"))
}
)
.listStyle(InsetGroupedListStyle())
}
func destroyWishlist() {
AF.request("http://localhost:3000/wishlist/\(wishlist.id)", method: .delete).responseJSON { response in
switch response.result {
case .success:
print("successful")
case let .failure(_):
print("error")
}
}
}
}
是否要转到上一个导航视图? 如果是,请尝试以下操作: 1。首先,添加一个环境变量。
@Environment(\.presentationMode) var presentationMode
self.presentationMode.wrappedValue.dismiss()
2。将以下代码放在您希望解雇的位置。
@Environment(\.presentationMode) var presentationMode
self.presentationMode.wrappedValue.dismiss()
例如:
struct WishlistView: View {
@Environment(\.presentationMode) var presentationMode
@State private var showingSheet = false
@State private var show_modal: Bool = false
let wishlist: Wishlist
var body: some View {
Text("Hello, Marketplace!")
.navigationBarTitle(wishlist.name)
.navigationBarItems(trailing:
HStack {
Button(action: {
showingSheet = true
}) {
Image(systemName: "ellipsis.circle.fill")
}
.actionSheet(isPresented: $showingSheet) {
ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
.default(Text("Delete Wishlist"), action: {
destroyWishlist()
}),
.default(Text("Green")),
.default(Text("Blue")),
.cancel()
])
}
.foregroundColor(Color("Rye"))
}
)
.listStyle(InsetGroupedListStyle())
}
func destroyWishlist() {
AF.request("http://localhost:3000/wishlist/\(wishlist.id)", method: .delete).responseJSON { response in
switch response.result {
case .success:
print("successful")
self.presentationMode.wrappedValue.dismiss()
case let .failure(_):
print("error")
}
}
}
}
您正在查找
\.presentationMode
上的discouse()
?