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
Andrés Leal
9,563 PointsHow can I use a func or var in any file in swift?
I want to use some variables in another viewController in the same project, I have tried to use public but it shows an error -> "non local" or something like that, please help!
8 Answers
Rasmus Rønholt
Courses Plus Student 17,358 PointsIm shutting down... You should look into the prepareForSegue method on UIViewController - how to override it to pass data between views. If you still are having problems, I will provide more detail at another time. Best of luck!
Rasmus Rønholt
Courses Plus Student 17,358 PointsYou can declare global functions and variables by simply putting them outside the scope of your classes - meaning, if you put them just after the import statements in ANY of the files in your project, then they are globally available - Xcode won't be able to help you auto complete the names though.
In most cases this is NOT a very good idea though, for a number of reasons. In most scenarios I would suggest you look into passing the data between controllers through segues. But there are some scenarios, where that is not the best way to go. What kind of variables and functions are we talking about - meaning, what do they represent in your application? User data stuff? Current application state stuff?
Andrés Leal
9,563 PointsThanks! What I want to accomplish is to get a random value for a UISlider and so the user can see how it changes I will add an animation to the UISlider to see how they move to the random value it assign, but the problem is that I have the UISlider and the random number in the first ViewController, and the button I want to tap and then make the animation of the UISlider, but I want to access the UISlider and the random number function form my second ViewController2 so I con perform the animation, I a stuck in this problem! :/
Rasmus Rønholt
Courses Plus Student 17,358 PointsAre both views displayed on screen at the same time? Or is it more like - user clicks button -> view switches and slider animates?
Andrés Leal
9,563 PointsUser clicks button -> view switches and slider animates, that is what I want to do.
Rasmus Rønholt
Courses Plus Student 17,358 PointsHow do you switch views? Are you using a segue or manually initialising and pushing? Should that view always animate to a random value, or ONLY when that particular button is pressed?
Andrés Leal
9,563 PointsSo My app is a picking color, so you choose RGB in the main menu and then it would change to the RGB value selector which is controlled with 3 UISliders one Red, one for Green and other for Blue, so I want to change them to a random value so it doesn't look ugly in the default color which is 125 for red, green and blue, which is a grey color, and it is kind of ugly to see that every time you choose the RGB color picker, so I created a getRandomColor() function that allows me to get a random color, and then apply it to the sliders, but I want to create the animation so the user see what happen, but the problem is that I have the UISliders and getRandomColor() in the first ViewController and the Button to make the action in the second "ViewController2", I want to access the sliders and the randomColor so I can apply the animation to the sliders when I tap on the RGB button. (the segue I created is on the storyboard, Not coded)
Andrés Leal
9,563 PointsThank you a lot Rasmus!! I will try that! :D
Andrés Leal
9,563 PointsAndrés Leal
9,563 PointsHey! Can you give me more details please? I still can't accomplish this :/
Rasmus Rønholt
Courses Plus Student 17,358 PointsRasmus Rønholt
Courses Plus Student 17,358 PointsLet me know if this makes sense to you...
To avoid issues with your destination view not being ready, or the animation to be half finished before the segue is complete, I would suggest you select the second option so you can do something like this.
Andrés Leal
9,563 PointsAndrés Leal
9,563 PointsThank you A LOT! This worked! :D
Also I have a problem with my navigation bar, it doesn't appear at run time but in the story board it does, any ideas why?
Rasmus Rønholt
Courses Plus Student 17,358 PointsRasmus Rønholt
Courses Plus Student 17,358 PointsThat's good to hear :)
The navigation bar can cause som peculiar problems, especially if trying to modify the appearing in interface builder. You should post a new question, and try to be more specific about how you are implementing the navigation bar.