-1

I want to know how can I sum the values of 2 WKInterfaceSlider.

This is the entire code I've done so far:

@IBOutlet weak var label1: WKInterfaceLabel!
@IBOutlet weak var slider1: WKInterfaceSlider!

@IBOutlet weak var label2: WKInterfaceLabel!
@IBOutlet weak var slider2: WKInterfaceSlider!


@IBAction func sliderValueChanged(value: Float)
{
    let roundedValue = Int(round(value))
    self.label1.setText("\(roundedValue)")
}

@IBAction func slider2ValueChanged(value: Float)
{
    let roundedValue = Int(round(value))
    self.label2.setText("\(roundedValue)")
}

@IBAction func calculos()
{
    var result = slider1.value + slider2.value
    self.result.setText("\(result)")
}

Xcode wont compile it, there is an ! that says

Value of type 'WKInterfaceSlider' has no member 'value'.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Vladimir
  • 77
  • 2
  • 11
  • 4
    show some code and what you have tried so far – luk2302 Dec 20 '15 at 10:24
  • 1
    Well, I tried this: @IBAction func calculos(){ var result = slider1.value + slider2.value self.result.setText("\(result)")} – Vladimir Dec 20 '15 at 10:35
  • 1
    and what happens then? Include that code in the question, completed with what the output *is* and what you would want it to be instead. – luk2302 Dec 20 '15 at 10:36
  • I don't know how to edit my question, but the result is that it simple does not works. I'm developing this for the Apple WatchOS 2 and xcode says: Value of type 'WKInterfaceSlider'has a member of 'value' – Vladimir Dec 20 '15 at 10:38
  • use the `edit` button underneath the question - and is the first time you mention watchos, that is a pretty important information here. – luk2302 Dec 20 '15 at 10:39
  • 1
    a general tip: read the docs https://developer.apple.com/library/watchos/documentation/WatchKit/Reference/WKInterfaceSlider_class/index.html#//apple_ref/occ/instm/WKInterfaceSlider/setValue: – luk2302 Dec 20 '15 at 10:41
  • 2
    good job improving your question, now it is clear what you have tried, what is going wrong and the solution is similarly clear. (note all the removed downvotes and even some (mine) upvotes) – luk2302 Dec 20 '15 at 10:52

1 Answers1

2

On WatchOS you have no way to read the slider value, you have to keep it track of it by yourself:

var sliderOneValue : Int = 0 // if you set starting values in the interface builder, you need to set them here as well
var sliderTwoValue : Int = 0 

@IBAction func sliderValueChanged(value: Float) {
    sliderOneValue = Int(round(value))
    self.label1.setText("\(sliderOneValue)")
}

@IBAction func slider2ValueChanged(value: Float) {
    sliderTwoValue = Int(round(value))
    self.label2.setText("\(sliderTwoValue)")
}

@IBAction func calculos() {
    let result = sliderOneValue + sliderTwoValue
    self.result.setText("\(result)")
}

The apple docs state

When the user changes the value of a slider, WatchKit delivers the new value to the slider’s action method. The format of a slider’s action method is as follows:

@IBAction func sliderAction(value: Float)

Declare a method of this form in the interface controller class used to receive the slider’s new value. You can change the method name to anything you like. When configuring the slider in Xcode, connect its selector to your custom action method.

Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    You are welcome, feel free to leave an upvote (you have enough reputation by now) and accept the answer :) – luk2302 Dec 20 '15 at 11:00