iOS Question How do I dynamically add segments to a segment control

davepamn

Active Member
Licensed User
I need to add a "Yes" and "No" segment to an segment control.

I need to know dynamically which segment the user selected.
 

Brian Robinson

Active Member
Licensed User
You have to check the SelectedIndex property.

Here is a code snippet from the CustomDialog class I sent through to you another time.

B4X:
Private Sub BuildReturnValues As Map
    Dim mp As Map
    mp.Initialize
    For Each ctl As View In ControlPanel
        If ctl.Tag <> "" Then
            If ctl Is TextField Then   
                Dim tf As TextField = ctl
                mp.Put(ctl.Tag, tf.Text)
            Else If ctl Is SegmentedControl Then
                Dim sc As SegmentedControl = ctl
                Dim lst As List = sc.GetItems
                mp.Put(ctl.Tag, lst.Get(sc.SelectedIndex))
            End If
        End If
    Next
   
    Return mp
End Sub
The code around the SegmentedControl can be called from either the "Click" or "IndexChanged" events.

Cheers
Brian
 

davepamn

Active Member
Licensed User
B4X:
Private arrSegmentControl(100) As SegmentedControl
...
arrSegmentControl(iSegmentControlCounter).Initialize("seg"&iSegmentControlCounter)
arrSegmentControl(iSegmentControlCounter).tag=sTestId

Dim oSCItems() As String=Array As String("Yes","No")

arrSegmentControl(iSegmentControlCounter).setitems(oSCItems)

If sTestIdValue="True" Then
        arrSegmentControl(iSegmentControlCounter).SelectedIndex=0
Else
       arrSegmentControl(iSegmentControlCounter).SelectedIndex=1
End If
Here is the code that worked
 
Last edited:

Brian Robinson

Active Member
Licensed User
Hi,

I have seen a few of your posts about using the segmented control.

Did you think of using a switch instead if it is only for a Yes/No. A switch is more like a checkbox, whereas a segmented control would be more like a radio button group, where you can have more than two responses?

Without knowing your complete project, I might be way off, but thought it might be worth considering?

Also, I think your answer will confuse others as it actually answers a different question.

The heading of the post is
"How do I dynamically add segments to a segment control"
Then inside the post
"I need to know dynamically which segment the user selected."

The code in your answer instead defaults in the selected item based on a value (Yes/No).

Not having a go at you, but if someone searches through the posts looking to solve a similar problem, this will confuse them.

To answer the questions though:
"How do I dynamically add segments to a segment control"

A segmented control accepts a List to set it's items. You can just pass a string array created dynamically whether you create it or it comes from say a iSQL query
B4X:
Dim sg As SegmentedControl

sg.Initialize("")

sg.SetItems(Array As String("Car","Motorcycle")) ' or any list built dynamically
"I need to know dynamically which segment the user selected."
To work out which segment was selected you use the IndexChanged event

B4X:
Private Sub sc_IndexChanged (Index As Int)

Msgbox("Item pressed: " & Index,"Item")

End Sub
If you don't need to know the index value right away (through the event), it can be worked out later with

B4X:
    ' Index key
    sc.SelectedIndex

     ' Text value
     sc.GetItems().Get(sc.SelectedIndex)
Like I said, not trying to be picky, but it makes it hard on others if the answer marked as correct does not match the question

Cheers
Brian
 
Top