ios - Create many UISliders that update a related label using one function -
i have experience user rating product, dragging sliders. each slider has related uilabel title , uilabel value. avoid creating 12 functions, 1 each slider , associated label. new development generally. guessing class or array useful here, not sure how use either. here's code updates 1 value, , know why is, i'm hoping avoid declaring 12 variables value , 12 functions each one.
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var slider1: uislider! @iboutlet weak var value1: uilabel! override func viewdidload() { super.viewdidload() } @ibaction func sliderslide(sender: uislider) { value1.text = round(sender.value*100).description } override func didreceivememorywarning() { super.didreceivememorywarning() } }
well,you can use outlet collection
, 'tag',
- drag every
label
sameoutlet collection
drag every
slider
ibaction same functionthen set tag of slider index of label in
outlet collection
. example,you first draglabela
collection,then tag 0
then code
import uikit class viewcontroller: uiviewcontroller { @iboutlet var labels: [uilabel]! override func viewdidload() { super.viewdidload() } @ibaction func sliderslide(sender: uislider) { let index = sender.tag let label = labels[index] label.text = round(sender.value*100).description } override func didreceivememorywarning() { super.didreceivememorywarning() } }
Comments
Post a Comment