Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Problems with returning a value from a method in one class, and using the most updated value in another class

Hey,

I have a function in a class that returns a boolean value TRUE only if a button is clicked. I also have another class that has a function that requires to know this boolean value.

To be able to access the boolean value, i have my variable declared as global and initialized to FALSE, and i change its value in the function of the first class.

I access this variable perfectly fine, but I just never get the updated value in the second class. I checked to see if my code enters the function, and it does. I think I am having problems returning the most updated value to the second class. I only get the initialized value of the bool variable.

CODE IN FIRST CLASS:

      var cookButtonClicked = false

      @IBAction func Cook(sender: AnyObject) {
        cookClicked()
        }

      func cleanerClicked() -> Bool{

        cleanerButtonClicked  = true
        return cleanerButtonClicked
        }

CODE IN SECOND CLASS:

     if (laborViewController.cleanerButtonClicked.boolValue){
    print("Success")
      }else{
           print("wrong value passed")
       }

Thanks

2 Answers

HeyYouseef,

None of the code you posted seems to be making sense?

First you posted

var cookButtonClicked = false

Which creates a variable that stores a bool set to false.

The second thing you posted was your IBAction that when pressed calls the function 'cookClicked'

@IBAction func Cook(sender: AnyObject) {
       cookClicked()
}

I can't see what this method does as you have not posted the function in your code example.

The third thing posted is the function 'cleanerClicked' which from your example looks like it's never called.

func cleanerClicked() -> Bool{
        cleanerButtonClicked  = true
        return cleanerButtonClicked
 }

If you've simply done a mistake with your naming that's your problem, we all make mistakes sometimes.

Otherwise it could be that you simply cannot simply access a variable by stating the viewController in which it sits. You need to use 'prepareForSegue' which allows you to pass variables between ViewControllers. You would simply update your bool in your function, check to see that it's updated and then pass the updated variable to your second ViewController. If you need to pass it back it gets a bit tricker and involves delegates which really aren't fun!

Thanks, Alex

Hey Alex,

Yea sorry i copied the wrong function .I meant to copy the 'cookClicked()' function which is exactly the same.

I'll try to use prepareForSegue to move the value of the variable and I'll let you know what happens

Thank You Yousef

Hey Alex,

I tried prepareForSegue() and it works well thank you very much!

I ran into a problem however when trying to send the data between three view controllers. I have VC1, VC2, and VC3. VC1 &VC2 are connected to the same class, and VC3 is connected to a second class. When trying to send the data related to VC1 to VC3, my App crashes.

Now I know I can't send directly between two View Controllers if there is a view controller in between. But would I be able to do it if I have the first View Controllers associated to the same class? Theoretically it would make sense to me.

Thank You for you help,
Yousef

Hey Yousef,

You shouldn't have Views connected to the same View Controller. That's a really bad idea. You can't segue within the same View Controller as it can only be used to send information between View Controllers, but not backwards. If you need to send data back you'll need to use a delegate which isn't fun.

Thanks, Alex

Hey Alex, I dot it work! thank you very much for your help!

I ended up having three Vcs: VC1 connect to VC2 which is connect to VC3. I then segue the data from VC1 to VC2. Then VC2 to VC3. It works well! Am running into problems regarding the navigation now: when a user goes back from VC3 to VC2 or VC1, my variables are not reinitializing. I Am trying to figure out init() with swift right now.

Thank you for your time! Yousef