-1

I have a problem to execute a function in a viewController from other viewController

I have two view controllers one called

ubicacionContainerViewController

and other

ReservaViewController

so I want to call a function in the reservaViewController from ubicacionContainerViewController

so in the ubicacionContainerViewController I have this code:

let storyboard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let reservaViewController: ReservaViewController = 
storyboard.instantiateViewController(withIdentifier: "ReservaViewController") as! 
      ReservaViewController

reservaViewController.cambiarContainer()

I used the code above to call a function in ReservaViewController and all is ok it is called

but now in ReservaViewController I have theses buttons

 @IBOutlet weak var btnSelectUbicacion: UIButton!
 @IBOutlet weak var btnSelectServicio: UIButton!
 @IBOutlet weak var btnSelectHora: UIButton!
 @IBOutlet weak var btnConfirmacionReserva: UIButton!

and this function:

func cambiarContainer() {
    self.btnSelectUbicacion.setBackgroundImage(#imageLiteral(resourceName: 
             "fondo_transparente"), for: .normal) // here is the error
    self.btnSelectServicio.setBackgroundImage(nil, for: .normal)
    self.btnSelectHora.setBackgroundImage(#imageLiteral(resourceName: 
            "fondo_transparente"), for: .normal)
    self.btnConfirmacionReserva.setBackgroundImage(#imageLiteral(resourceName: 
             "fondo_transparente"), for: .normal)
}

so the problem is when I press a button in ubicacionContainerViewController to call appear that error.

I have tried using the "?"

func cambiarContainer(posicion: Int) {        
    self.btnSelectUbicacion?.setBackgroundImage(#imageLiteral(resourceName: 
             "fondo_transparente"), for: .normal) //no the error disapear
    self.btnSelectServicio?.setBackgroundImage(nil, for: .normal)

    self.btnSelectHora?.setBackgroundImage(#imageLiteral(resourceName: 
            "fondo_transparente"), for: .normal)
    self.btnConfirmacionReserva?.setBackgroundImage(#imageLiteral(resourceName: 
             "fondo_transparente"), for: .normal)
}

now all is ok it works ok but the problem is that the background image of the buttons does not work . nothing change.

Maciej Gad
  • 1,701
  • 16
  • 21
  • See https://stackoverflow.com/a/29322364/1226963 - you are calling `cambiarContainer()` too soon. – rmaddy Nov 06 '19 at 17:53
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Rey Bruno Nov 06 '19 at 17:55
  • so where can i call the function while ReservaViewController is loaded ? – David Garcia Nov 06 '19 at 18:06

2 Answers2

0

At the moment you call cambiarContainer the outlets are not connected yet and therefore the code crashes.

Delete

reservaViewController.cambiarContainer() 

and move the code to modify the UI into viewDidLoad

func viewDidLoad() {
   super.viewDidLoad() 
   self.btnSelectUbicacion.setBackgroundImage(#imageLiteral(resourceName: "fondo_transparente"), for: .normal) // here is the error
   self.btnSelectServicio.setBackgroundImage(nil, for: .normal)
   self.btnSelectHora.setBackgroundImage(#imageLiteral(resourceName: "fondo_transparente"), for: .normal)
   self.btnConfirmacionReserva.setBackgroundImage(#imageLiteral(resourceName: "fondo_transparente"), for: .normal)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
-1

the problem here is that you have already instantiated your ReservaViewController but its view have not been loaded yet, and the cambiarContainer method its calling one or more of the (currently not loaded) outlets of the controller; that's why it's pointing you that the references are nil.

Try loading the view before calling the cambiarContainer method, by adding the following line:

_ = reservaViewController.view
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    This solution is more of a hack. Just call `cambiarContainer()` after calling `present` or `show` or `push`. – rmaddy Nov 06 '19 at 18:32
  • _ = reservaViewController.view works all ok but nothing change what i want i dont know why, the app is all ok does not crash but the buttons does not change nothing. – David Garcia Nov 06 '19 at 18:55
  • how can i use present, show or push? if my ubicacionContainerViewController is a containerView that is in the ReservaViewController , can you understand me? – David Garcia Nov 06 '19 at 18:57
  • dear @maddy i used self.present(reservaViewController, animated: true, completion: nil) but i have the same error Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value – David Garcia Nov 06 '19 at 19:14
  • This is a hacky workaround that doesn't address the underlying problem. Rather than the current approach, why not configure the buttons from `viewDidLoad` by calling `cambiarContainer()` there? This way you know your storyboard will be fully instantiated by the time you try to configure. – flanker Nov 06 '19 at 19:22