exc_breakpoint 尝试在 SwiftUI 中加载视图时
exc_breakpoint while trying to load view in SwiftUI
我正在尝试制作条形码扫描器应用程序。我希望扫描仪在应用程序首次启动时加载,就像背景一样。然后当扫描条形码时,在顶部加载一个视图来显示产品。
在我的 ContentsView() 中加载此视图,它会启动扫描仪,然后在找到条形码后导航到 FoundItemSheet()。
import Foundation
import SwiftUI
import CodeScanner
struct barcodeScannerView: View {
@State var isPresentingScanner = false
@State var scannedCode: String?
@State private var isShowingScanner = false
var body: some View {
NavigationView {
if self.scannedCode != nil {
NavigationLink("Next page", destination: FoundItemSheet(scannedCode: scannedCode!), isActive: .constant(true)).hidden()
}
}
.onAppear(perform: {
self.isPresentingScanner = true
})
.sheet(isPresented: $isShowingScanner) {
self.scannerSheet
}
}
var scannerSheet : some View {
CodeScannerView(
codeTypes: [.qr],
completion: { result in
if case let .success(code) = result {
self.scannedCode = code
self.isPresentingScanner = false
}
}
)
}
}
当导航视图被按钮替换时,像这样:
VStack(spacing: 10) {
if self.scannedCode != nil {
NavigationLink("Next page", destination: FoundItemSheet(scannedCode: scannedCode!), isActive: .constant(true)).hidden()
}
Button("Scan Code") {
self.isPresentingScanner = true
}
.sheet(isPresented: $isPresentingScanner) {
self.scannerSheet
}
Text("Scan a QR code to begin")
它适用于此解决方案,但我希望扫描仪在应用加载时显示,而不是在按下按钮时显示。我尝试用与按钮内容相同的 .onAppear 替换按钮,但它不起作用。
这是找到的ItemSheet()
struct FoundItemSheet: View {
@State private var bottomSheetShown = false
@State var scannedCode: String?
var body: some View {
GeometryReader { geometry in
BottomSheetView(
scannedCode: self.$scannedCode,
isOpen: self.$bottomSheetShown,
maxHeight: geometry.size.height * 0.7
) {
Color.blue
}
}.edgesIgnoringSafeArea(.all)
}
}
struct FoundItemSheet_Previews: PreviewProvider {
static var previews: some View {
FoundItemSheet()
}
}
我得到 exc_breakpoint 我相信 CodeScanner 的声明位置。
我已经被困在这个问题上几个小时了,所以我会快速回复任何问题。
一些想法..
1) 改变修饰符的顺序
.sheet(isPresented: $isShowingScanner) {
self.scannerSheet
}
.onAppear(perform: {
self.isPresentingScanner = true
})
2) 延迟 sheet 激活(因为视图层次结构可能尚未为该自定义扫描仪视图准备好)
.onAppear(perform: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
self.isPresentingScanner = true
}
})
我正在尝试制作条形码扫描器应用程序。我希望扫描仪在应用程序首次启动时加载,就像背景一样。然后当扫描条形码时,在顶部加载一个视图来显示产品。
在我的 ContentsView() 中加载此视图,它会启动扫描仪,然后在找到条形码后导航到 FoundItemSheet()。
import Foundation
import SwiftUI
import CodeScanner
struct barcodeScannerView: View {
@State var isPresentingScanner = false
@State var scannedCode: String?
@State private var isShowingScanner = false
var body: some View {
NavigationView {
if self.scannedCode != nil {
NavigationLink("Next page", destination: FoundItemSheet(scannedCode: scannedCode!), isActive: .constant(true)).hidden()
}
}
.onAppear(perform: {
self.isPresentingScanner = true
})
.sheet(isPresented: $isShowingScanner) {
self.scannerSheet
}
}
var scannerSheet : some View {
CodeScannerView(
codeTypes: [.qr],
completion: { result in
if case let .success(code) = result {
self.scannedCode = code
self.isPresentingScanner = false
}
}
)
}
}
当导航视图被按钮替换时,像这样:
VStack(spacing: 10) {
if self.scannedCode != nil {
NavigationLink("Next page", destination: FoundItemSheet(scannedCode: scannedCode!), isActive: .constant(true)).hidden()
}
Button("Scan Code") {
self.isPresentingScanner = true
}
.sheet(isPresented: $isPresentingScanner) {
self.scannerSheet
}
Text("Scan a QR code to begin")
它适用于此解决方案,但我希望扫描仪在应用加载时显示,而不是在按下按钮时显示。我尝试用与按钮内容相同的 .onAppear 替换按钮,但它不起作用。
这是找到的ItemSheet()
struct FoundItemSheet: View {
@State private var bottomSheetShown = false
@State var scannedCode: String?
var body: some View {
GeometryReader { geometry in
BottomSheetView(
scannedCode: self.$scannedCode,
isOpen: self.$bottomSheetShown,
maxHeight: geometry.size.height * 0.7
) {
Color.blue
}
}.edgesIgnoringSafeArea(.all)
}
}
struct FoundItemSheet_Previews: PreviewProvider {
static var previews: some View {
FoundItemSheet()
}
}
我得到 exc_breakpoint 我相信 CodeScanner 的声明位置。
我已经被困在这个问题上几个小时了,所以我会快速回复任何问题。
一些想法..
1) 改变修饰符的顺序
.sheet(isPresented: $isShowingScanner) {
self.scannerSheet
}
.onAppear(perform: {
self.isPresentingScanner = true
})
2) 延迟 sheet 激活(因为视图层次结构可能尚未为该自定义扫描仪视图准备好)
.onAppear(perform: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
self.isPresentingScanner = true
}
})