【发布时间】:2020-03-28 01:26:34
【问题描述】:
我遇到了非常奇怪的错误unexpectedly found nil while unwrapping an optional value navigationcontroller。
我有两个视图控制器,其主要功能是扫描条形码并在 PreBarCodeViewController 上的 textField 中设置值。第二个viewController是扫描的barcodeViewController。根据透视用户手动输入值或扫描以获取条形码。当我去手动输入导航控制器工作正常。但是,一旦我单击扫描并在textField 中获取值并按确定按钮,它将无法导航。即使我单击十字按钮,它也会给我零错误。
更新:包括在按钮点击时打开的条形码视图控制器。
PreBarcodeViewController:两个按钮返回和接受
@IBAction func toBarcode(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "BarcodeViewController") as! BarcodeViewController
present(newViewController, animated: true, completion: nil)
}
@IBAction func onClickedBackArrow(_ sender: Any) {
self.navigationController!.popViewController(animated: true)
}
@IBAction func onClickedAccept(_ sender: Any) {
let value = textField.text ?? String(describing: txtValue)
bardelegate?.listFunc(listValue: value )
self.navigationController?.popViewController(animated: true)
}
BarcodeViewController:当我把值贴到后面。
if url == decodedURL {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "PreBarcodeViewController") as! PreBarcodeViewController
let str2 = String(url)
newViewController.txtValue = str2
present(newViewController, animated: true, completion: nil)
}
【问题讨论】:
-
你是怎么打开
BarcodeViewController的?? -
@dahiya_boy 更新了代码。点击按钮。
-
在
BarcodeViewController中,您需要Dismiss 您的控制器并使用委托来获取您之前的控制器中选择的数据。 -
@dahiya_boy 在
PreBarcodeController哪里设置委托?viewDidAppear-barscan.delegate = self? -
现前
BarcodeViewController.
标签: ios swift uiviewcontroller uinavigationcontroller