i have uilabel in app display chat messages. these chat messages hardcoded array of strings. need display these messages 1 one on uilabel need have 2 second delay between switching messages user need read each message.
what have implemented following code:
for chattext in chatdialogue{ dispatch_async(dispatch_get_main_queue(), { chatlabel.text=chattext as? string }) println(chattext) nsthread .sleepfortimeinterval(2) }
here have array of chatdialogues
["hello","how you?","can dias","wait please"]
now need these displayed on same label delay between changing messages.
the above implementation when executed showed last message on loop.
set nstimer
fires every 2 seconds , stops when reaching last text:
// instance variables let chatdialoges = ["hello","how you?","can dias","wait please"] var textindex = 0 var timer: nstimer? // ... // somewhere timer should start self.timer = nstimer.scheduledtimerwithtimeinterval(2, target: self, selector: "update", userinfo: nil, repeats: true) // func update() { self.textlabel++ if self.textlabel == chatdialoges.count { self.timer?.invalidate() return } chatlabel.text = chatdialoges[self.textindex] }
Comments
Post a Comment