我的目标是让用户点击一个按钮,然后让他们选择是或取消,这工作正常。如果选择是,它应该移动到相机视图。我得到错误:未使用"NavigationLink〈Label,Destination〉"初始值设定项的结果。
struct ContentView: View {
@State private var showAlert = false
var body: some View {
NavigationStack
{
VStack {
Button{
showAlert = true
} label: {
Text("+")
}
.frame(width: 40, height: 40)
.symbolVariant(.fill)
.background(.red)
.cornerRadius(15)
.foregroundColor(.white)
.padding(.trailing,300)
Spacer()
}
.alert("Create Event Here?", isPresented: $showAlert) {
Button("Yes"){NavigationLink("addCameraView", destination: CameraView())//*****gets the error
}
Button("Cancel", role: .cancel) { }
}
}
struct CameraView: View{
@State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
@State private var selectedImage: UIImage?
@State private var imagePickerDisplay = false
var body: some View {
NavigationView {
VStack {
if selectedImage != nil {
Image(uiImage: selectedImage!)
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
} else {
Image(systemName: "snow")
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
}
Button("Camera") {
self.sourceType = .camera
self.imagePickerDisplay.toggle()
}.padding()
}
.navigationBarTitle("Take a Photo of the Event")
.sheet(isPresented: self.$imagePickerDisplay) {
ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
}
}
}
}
}
1条答案
按热度按时间a1o7rhls1#
要使用按钮进行导航,我们需要使用一个变量作为触发器,将按钮 Package 在
NavigationLink
中并将相关变量更新为适当的值将触发导航。下面是更新后的
ContentView
。CameraView
保持不变。