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

How 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

Im 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!

Hey! Can you give me more details please? I still can't accomplish this :/

Let me know if this makes sense to you...

//In view controller A (the one with your button)

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        //This method will run just before any segue on this viewcontroller is performed.
        //So the first thing you do is to check exactly what segue is going to run.
        //So you need to give it an identifier in the storyboard,
        //and replace "YourSegueIdentifier".
        if segue.identifier == "YourSegueIndentifier" {

            //Next you tell xcode what type of viewcontroller is on the other end of the segue
            //You do this by downcasting the destinationViewController with (as!)
            //Replace ClassName with the actual name of your UIViewController subclass
            let yourView = segue.destinationViewController as! ClassName

            //Now you have access to your view with the slider and you can either

            //1. Set properties and call the methods you need to directly from here
            yourView.color = self.randomColorForNewView
            yourView.animateSliderToColor()

            //2. Set one boolean property telling the view how to act when it comes alive
            yourView.shouldAnimateToRandomColor = true

        }
    }

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.

//In view controller B (the one with your slider)

    var shouldAnimateToRandomColor: Bool = false

    override func viewDidAppear(animated: Bool) {
        if self.shouldAnimateToRandomColor {
            //Get a random color and perform animation
            self.shouldAnimateToRandomColor = false
        }
    }

Thank 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?

That'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.

You 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?

Thanks! 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! :/

Are both views displayed on screen at the same time? Or is it more like - user clicks button -> view switches and slider animates?

User clicks button -> view switches and slider animates, that is what I want to do.

How 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?

So 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)

Thank you a lot Rasmus!! I will try that! :D