3

I'm new to Swift and iOS in general. I'm using Swift to write an app. This app has two files, ViewController.swift and BTService.swift.

ViewController.swift has a class ViewController of type UIViewController, and BTService.swift has a class BTService of types NSObject and CBPeripheralDelegate. I have a slider set up in the UIViewController class, and its value is assigned to variable currentValue.

Now, I want to be able to reference currentValue from within the BTService class. How can I go about doing this? I've noticed that if I define a variable, test, in the file ViewController before the class UIViewController, that I can reference test in BTService. But that's of no use to me since I can't (to my knowledge) get the slider value to be assigned to test unless test is defined within the UIViewController class.

Here's my ViewController and the currentValue variable definition.

class ViewController: UIViewController {


@IBOutlet var positionLabel: UILabel!
@IBOutlet var positionSlider: UISlider!
@IBOutlet var connectionLabel: UILabel!

@IBAction func sliderValChanged(sender: UISlider) {

    var currentValue = Float(positionSlider.value)

Any help would be greatly appreciated! Thanks.

2 Answers 2

1

You can use NSUserDefaults to store data within your application and share it between view controllers.

In the example you give, you could store it like this:

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setFloat(Float(positionSlider.value), forKey: "sliderValue") // Write to NSUserDefaults
defaults.synchronize()

Then, whenever you need access to it in another file (or in this one), you can just call floatForKey:

if let currentValue: Float = defaults.floatForKey("sliderValue") {
    // Access slider value
} else {
    // Probably not saved
}
Sign up to request clarification or add additional context in comments.

13 Comments

If i put the line let currentValue: Float = defaults.floatForKey("sliderValue") in the BTService class, it doesn't recognize the name "defaults". I defined defaults in the ViewController class. is this what i'm supposed to do?
@RossM. If you want to access it from the BTService class, you have to define defaults again (just copy and paste the first line) in that class before you try to access it using floatForKey.
okay that works! problem is that it only shows the initial value for the slider, but doesn't update the variable with the new value if you change it. the only way i could think of how to get it to update would be to reference sliderPosition but that produces the same problem that the NSUserDefaults method fixed. is there something really simple i'm missing?
@RossM. Get rid of all references to the value of the slider and use NSUserDefaults in its place. Put that first code block inside of sliderValChanged so that it updates whenever the slider changes.
"Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey: will be immutable."--from the swift documentation. Am I misunderstanding this, or does it basically say that my NSUserDefaults can't change?
|
0

Is your BTService a property within the UIViewController?

Could you use Key Value Observing (KVO)

Inside your UIViewController

override func viewDidLoad() {
    self.addObserver(self, forKeyPath: "positionSlider.value", options: .New, context: nil)
}

deinit() {
    self.removeObserver(self, forKeyPath: "positionSlider.value")
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == "positionSlider.value" {
        // update service property
    }
}

1 Comment

it's not a property actually, it's a separate class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.