0

I am facing very weird error unexpectedly found nil while unwrapping an optional value navigationcontroller.

I have two viewControllers which has main function is to scan bar code and sets value in textField on PreBarCodeViewController. Second viewController is the barcodeViewController which scan. According to perspective user either manually input value or scan to fetch bar code. When I go for manual input navigation controller works fine. But once I click for scan and gets value in a textField and press ok button it won't navigate. Even if I click cross button it gives me nil error.

Updated: included barcode viewcontroller open on button tap.

PreBarcodeViewController: two buttons Back and Accept

@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: When I get the value post to back.

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)

        }
  • How you opening `BarcodeViewController` ?? – dahiya_boy Dec 03 '19 at 11:15
  • @dahiya_boy updated code. On button Click. –  Dec 03 '19 at 11:21
  • In `BarcodeViewController`, you need to [Dismiss](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss) your controller and use delegate to get selected data in your previous controller. – dahiya_boy Dec 03 '19 at 11:24
  • @dahiya_boy On `PreBarcodeController`where I set delegate ? `viewDidAppear` - `barscan.delegate = self` ? –  Dec 03 '19 at 11:38
  • Before present `BarcodeViewController`. – dahiya_boy Dec 03 '19 at 11:39
  • @dahiya_boy on `BarcodeViewController` I used it and giving me error `Expression resolves to an unused property` I use delegate. Protocol created on `BarCodeViewController` –  Dec 03 '19 at 11:48
  • I think you knew how to make [protocol-delegate](https://stackoverflow.com/questions/59100588/selections-from-list-and-reload-tableview-swift) – dahiya_boy Dec 03 '19 at 11:50
  • @dahiya_boy Yes I know I created it like same way. –  Dec 03 '19 at 11:57

1 Answers1

0

When you open with present() view controller then used the dismiss() method to back to previous screen.

popViewController()

only used when you navigate with navigation controller please check this.

kishor soneji
  • 795
  • 1
  • 9
  • 16
  • fine but main thing is `onClickedAccept` button event. –  Dec 03 '19 at 11:24
  • `BarcodeViewController` not showing delegate assignment in available method please check this. – kishor soneji Dec 03 '19 at 11:28
  • I am retrieving value displaying on textField and then delegates it everything fine only when scans perform I got value in preBarCodeController but navigateController won't work. –  Dec 03 '19 at 12:23