How to receive value from a variable of another Class

Asked

Viewed 497 times

1

I have a class variable Class1 and I want to use this variable in a function in the class Class2

Tela1 has as its control the Class1 and tela2 has as control the Class2

when called to tela2, Class2 will execute a function whose parameter is the variable of the Class1

2 answers

2

There are several ways to do this, I believe the easiest way is to pass the value via attribute.

Declare in Viewcontroller1 as attribute the value you want to pass.

var numeroAleatorio = 4

In Viewcontroller 2, create an attribute that receives the value of Viewcontroller1.

var viewController1: ViewController!

As in this case you will NOT initialize the variable, you NEED to use the ! operator which means that the variable is optional. If you don’t use, Swift will ask you to initialize the variable which is not your intention, since it has already been started in Viewcontroller1.

The type of this variable has to be the class of its Viewcontroller1.

Now, let’s show on your screen your Viewcontroller 2 from Viewcontroller1.

let v2 = ViewController2()
v2.viewController1 = self

self.presentViewController(v2, animated: true, completion: nil)

Note that in

v2.viewController1 = self

you are pointing out the value of the viewController1 attribute you created in your Viewcontroller2 for your first view.

With the pointer to your first variable, it is extremely easy to access ANY attribute of the first view variable.

NSLog("%d", viewController1.numeroAleatorio)

If you start having two more views, it might be interesting to research Singleton.

I recommend you also study ways to persist your information like Coredata and Nsuserdefaults.

  • Thanks, Carl, I’ll test it here. for Coredata I chose not to save the value because it is temporary, what will be saved is the result of the function that is in View2

  • gave error v2.viewController1 = self that v2 cannot receive uncle v1

  • you set v1 as Uiviewcontroller or as Viewcontroller? The type of v2.viewController has to be the type of your first view.

  • Sorry Carl, as I’m starting at IOS I ended up getting confused, the classes are Uiviewcontroller type.. I’ll rephrase the question

  • What is the name of your first view? Pass me the line you declared the attribute viewController1

  • edited the question again

  • 1

    Fabio, look for prepare, bemmmmmmmmmmm is easier.

  • 1

    @Jadsonmedeiros Thank you very much, at first it was a bit confusing the materials I found, but this link gives to understand very well http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/

Show 3 more comments

1


I resolved through the prepare as suggested by @Jadsonmedeiros learned through that James Leist’s website

1) I created a follow selecting the screen button and holding the CTRL key, I dragged the screen 2

1.1) Paragraph 1 Storyboard, I selected the following (arrow connecting the screens) and Attributes Inspector, in the field Identify placed Nomedasegue

2) in Classe1 to pass the variable, I added the function

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "NomeDaSegue" {
            var svc = segue.destinationViewController as! Classe2

            svc.variavelDaClasse2_1 = variavelDaclasse1_1
            svc.variavelDaClasse2_2 = variaveldaClasse1_2.text
        }
    }

3) in Classe2 where I will use the recovered variable

// dados da Classe1, não podem receber valores
    var variavelDaClasse2_1 : Int!
    var variavelDaClasse2_2 : String!


 print(variavelDaClasse2_2)
 print(variavelDaClasse2_1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.