How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Chris Baxter Your Own Question
Chris Baxter
Chris Baxter, Programming Tutor
Category: Programming
Satisfied Customers: 248
Experience:  Completed BSIT concentration Software Engineering, tutored other students and classmates in Java and VB.net, C++, and Obj C.
68869132
Type Your Programming Question Here...
Chris Baxter is online now
A new question is answered every 9 seconds

I am trying to update a label field with a loop in Swift

Customer Question

I am trying to update a label field with a loop in Swift programming language. Can you help?
Submitted: 1 year ago.
Category: Programming
Customer: replied 1 year ago.
I am trying to update the label on view controller two with the hours/minutes/seconds that have passed after the action button on view controller one is pressed ( xcode - swift prog language)
Expert:  Chris Baxter replied 1 year ago.

https://learnappmaking.com/add-1-creating-a-simple-ios-game-with-swift-in-xcode-week-3

Unfortunately there is a need to be inside your Xcode to draw reference links between the pages, you can find instructions at the link above with full explanations of what you need to do. (Including where to put it in your existing code) Please let me know if you need anything further!

Customer: replied 1 year ago.
You did not answer my question. Ive already made reference links in the file provided. I am trying to transfer data from one viewcontroller to another(view2). And have that update occur on a loop.
Expert:  Chris Baxter replied 1 year ago.

Is viewcontroller1 a parent (container) of view2?

Customer: replied 1 year ago.
There are two viewcontrollers. Mainviewcontroller and view2
Customer: replied 1 year ago.
Not sure what you mean by parent controller
Customer: replied 1 year ago.
I believe the data needs to be transferred via a protocol function and delegate. I need this to happen via a loop...not sure how to write this code
Expert:  Chris Baxter replied 1 year ago.

The timer cannot be stopped outside of the scope in which it is declared. However, you can send a message ( a notification) from another view controller back to the timer view controller to achieve the result you are looking for. You must send this notification from the controller with the timer declared in it, and it is from this view controller you will send a notification to stop(and start) the timer.

NSNotificationCenter.defaultCenter().postNotificationName("StopTimerNotification", object: nil)

However, you also have to tell the view controller where the timer is declared to listen for this type of notification which we named StopTimerNotification and what to do when it receives it. Note: Put this in your viewDidLoad method.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "StopTimerNoticationFunction:", name:"StopTimerNotification", object: nil)

Finally, you need to implement the code to stop the timer. In this example, we have named this callback StopTimerNotificationFunction. So. put this in your file where you have declared the timer.

func StopTimerNotication(notification: NSNotification){ //Put stop timer code here. } <<or start timer code

Expert:  Chris Baxter replied 1 year ago.

A loop is a loop, for timers they are relatively simple

while so and so is not equal to whatever end-point, increment.

@IBAction func start(sender: AnyObject) {

let aSelector : Selector = “updateTime”

timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)

startTime = NSDate.timeIntervalSinceReferenceDate()

}

"scheduledTimerWithTimeInterval" is your loop.

Expert:  Chris Baxter replied 1 year ago.

So instead of @buttonaction start, and selector named "update time" you simply want:

timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, userInfo: nil, repeats: true)

Expert:  Chris Baxter replied 1 year ago.

0.01 is the duration in milliseconds, of firing on that method, there is no "data" to transfer short of the "did it fire, yes, no" message

Customer: replied 1 year ago.
You still did not answer my question. The loop that I am referring to will constantly update the label field. The transfer of the data from the first view controller needs to happen on a loop. That code I have now just transfers it once when the action button is pressed. I am not concerned with starting or stopping the timer. I know how to do that

Related Programming Questions