UserGuide

iOS Controllers

From Xojo Documentation

Timer

iOS Timer Library Icon

A Timer is not actually a UI control, but it is often used with UI controls. A Timer runs code at periodic intervals, which is useful for updating progress, animation and anything else that requires the UI to be updated periodically.

Below are the common events, properties and methods. Refer to Xojo.Core.Timer in the Language Reference for for all its details.

Events

Action

This event is called when the Timer interval is reached, as specified by its Mode, Period and Tolerance.

Properties

Mode

The Mode of a Timer indicates how its Action event gets called. It can have one of three values from the Xojo.Core.Timer.Modes enumeration:
  • Off: The Action event is not called.
  • Single: The Action event is called once when the Period is reached.
  • Multiple: The Action event is called each time the Period is reached.

Period

Indicates (in milliseconds) how frequently the Action event is called.

Methods

CallLater

Used to set up a Timer that will call a method at when the Period is reached.

CancelCall

Used to cancel a Timer set up with CallLater.

Usage

When you add a Timer to a Layout, the Inspector shows the Mode and Period properties. As noted above, the Mode can be Off, Single or Multiple (the default). Period defaults to 1000 milliseconds (1 second).

A Timer is used to update the UI. Your app code runs in what is called the "main thread". Timer code also runs in the main thread. If you have a long-running process, it is possible that the Timer's Action event handler call could be delayed until the process finishes. Essentially, a Timer's Action event is only called when the app is idle. This is fine for nearly all cases because and app is most often in the idle state.

Another way to think of a Timer is that it is a "waiter". It waits at least until the Period has elapsed and the app is idle. When those conditions are met, it runs its Action event handler.

You can change the Mode and Period values within the Timer's Action event handler in order to change how often the Timer runs or to stop it from running entirely.

This code updates a Progress Bar every 1/2 second (Mode = Multiple and Period = 500) and then turns the Timer off when the Maximum of the Progress Bar is reached:

ProgressBar1.Value = ProgressBar1.Value + 1

// Stop Timer when ProgressBar reaches maximum
If ProgressBar1.Value > ProgressBar1.MaxValue Then
Me.Mode = Xojo.Core.Timer.Modes.Off
End If

Timers are also used for animation, typically to tell a Canvas to redraw itself. If you have code in a Canvas Paint event handler that updates the position of some graphics, you can tell it to redraw itself from a Timer:

AnimationCanvas.Invalidate

You can also add Timers directly in code, but when you do so you have to provide a method that will be called in place of the Action event handler. For a Timer you only want to call once, you can do this using the CallLater method. You create a method that you want the Timer to call, such as this method which will clear a Label's text:

Sub ClearLabel
MyLabel.Text = ""
End Sub

Then you use the CallLater method to tell the Timer to call this method after a period of time. This code sets the Label text and then tells it to call the ClearLabel method after two seconds so that it gets cleared:

MyLabel.Text = "Help text goes here"
Xojo.Core.Timer.CallLater(2000, AddressOf ClearLabel)

If you need a repeating Timer, you can create one and then assign a method to run in place of the Action event by using the AddHandler command. This code calls the TimerAction method once a second:

MyTimer = New Xojo.Core.Timer
MyTimer.Period = 1000
MyTimer.Mode = Xojo.Core.Timer.Modes.Multiple

AddHandler MyTimer.Action, AddressOf TimerAction

If you use AddHandler with a Timer, you also need to call RemoveHandler at some point. You'll usually want to do this when the Timer is stopped or when the Layout containing the Timer is closed.

Generic Object

Generic Object Library Icon

A Generic Object is used to add classes that are not UI controls to a Layout. These classes appear on the Shelf of the Layout. Adding classes in this manner makes it easier to implement their events. Because the class is now on the Layout, you can just click on it and select "Add Event Handler" to implement its event handlers. The alternative to this is creating a subclass, implementing the events on the subclass and then manually creating an instance via code.

After dragging a Generic Object onto your layout, you need to go to the Super field in the Inspector and change it to the name of the class you want to use.

See Also

Xojo.Core.Timer class; UserGuide:iOS UI topic