instantiate class to access properties from another SWIFT class

0

Look, I have time looking for how to instantiate class to access properties ... I've seen everywhere that they do this ...

  let vc = otroViewController()
  vc.label.backgroundColor = UIColor.black // por ejemplo una etiqueta para cambiar su fondo

I've also done this but it's obviously the same:

 var vc: otroViewController? = nil // incluso sin usar nil

and in viewdidload

 viewDidLoad() {

  vc = OtroViewController() // tambien "otroViewController.self"
                                  y     "self.otroViewController"
 }

but by doing this:

  let vc = otroViewController()

A NEW Viewcontroller is being created ... and therefore the properties are not what I need to call, and I could not call those properties of the class to access them and get the data of that class

I have several questions:
Do you need to create some initializer like get and set?
Does the class type matter? "either UICollectionViewCell or UIViewController, NSObject ... etc"
or use the delegates? "I have researched but it is not clear to me yet"
or use a segue? - > it exists to do it in a "programatical" way since I do not use Storyboards at all

Does anyone know about this or did the same thing happen to them?

    
asked by Yan Cervantes 09.12.2017 в 01:11
source

1 answer

0

You can use the SEGUES, they are useful to pass data between different Views:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{
        //Checar a que view controller se esta llamando por medio del identificador del segue
        if segue.identifier == “segue_identificador_controller”
        {
            if let destino = segue.destination as? ViewController2
            {
                //Pasar los datos
                destino.label.text = “Ejemplo”
            }
        }

}

//Para invocar al nuevo View controlar
self.performSegue(withIdentifier: “segue_identificador_controller", sender: nil)

And already with those functions you could work without problems.

    
answered by 12.12.2017 в 16:22