【问题标题】:navigationController unexpectedly found nil on SwiftnavigationController 在 Swift 上意外发现 nil
【发布时间】: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


【解决方案1】:

当您使用 present() 打开视图控制器时,然后使用 dismiss() 返回上一屏幕的方法。

popViewController()

仅在您使用导航控制器导航时使用,请检查此项。

【讨论】:

  • 很好,但主要是onClickedAccept按钮事件。
  • BarcodeViewController 未在可用方法中显示委托分配,请检查此。
  • 我正在检索 textField 上显示的值,然后仅在执行扫描时将其委托给它,我在 preBarCodeController 中获得了值,但 navigateController 不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2020-08-26
  • 1970-01-01
相关资源
最近更新 更多