Search code examples
swiftuiviewcontrollerstoryboardpresentviewcontroller

PresentViewController found nil value on instantiateViewController


 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView.tag == 2{

            switch indexPath.item {
            case 0:
               if let vc = storyboard?.instantiateViewController(withIdentifier: "offersScreen") as? OffersVC {
                vc.cityID = self.cityID!
                vc.categoryID = indexPath.item + 1
                vc.headingLBL.text =  self.names[indexPath.item]
                vc.cityLBL.text = self.selectCity.text!
                    present(vc, animated: true, completion: nil)
                }   
            case 1:
                print("You're heading VC 1!")
            case 2:
                print("You're heading VC 2!")
            case 3:
                print("You're heading VC 3!")
            default:
                print("Something went wrong")
            }
        }
    }

enter image description here

I wrote everything perfect idk what I made mistake. It found nil value on on present. I present viewcontroller from tabbar viewController with 4 values Please help or guide. Thanks in advance.


Solution

  • Reason is all outlets are nil until the vc loads

    options #1

     vc.loadViewIfNeeded()
     vc.headingLBL.text =  self.names[indexPath.item]
    

    options #2

    make vars of what you need to send

    class OffersVC:UIViewController {
       var sendedStr = ""
    

    then inside viewDidLoad

    self.headingLBL = sendedStr