loops - Looping Label Change Backround tast in Swift -


i'm new swift , looking way loop changing label cycles trough array of strings. ways i've tried have stopped other tasks while loop running.

you're view controller this:

class viewcontroller: uiviewcontroller {      @iboutlet weak var cyclelabel: uilabel!      var strings: [string]!     var timer: nstimer!     var index: int = 0      override func viewdidload() {         super.viewdidload()         self.strings = ["lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "vestibulum", "erat", "lacus", "congue"]     }      override func viewwillappear(animated: bool) {         super.viewwillappear(animated)         self.cyclelabel.text = self.strings[self.index]     }      @ibaction func begincyclingtapped(sender: uibutton) {         let interval = 1.0          if self.timer.valid {             self.timer.invalidate()         }          self.timer = nstimer.scheduledtimerwithtimeinterval(interval, target: self, selector: "updatelabel", userinfo: nil, repeats: true)     }      func updatelabel() {         self.index += 1         self.cyclelabel.text = self.strings[self.index % self.strings.count]     }  } 

this code update label text next string in strings property every 1 second. if you'd different interval, change interval constant in begincyclingtapped(:) method. label start restart beginning of strings array after reaches last element in array. if statement in begincyclingtapped(:) ensures multiple timers not scheduled update label, result in label getting updated more desired. also, make sure hook iboutlet uilabel on storyboard.


Comments

Popular posts from this blog

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -