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

Discussion in 'iOS Questions' started by davepamn, Mar 18, 2015.

  1. davepamn

    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.
     
  2. Brian Robinson

    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.

    Code:
    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
     
  3. davepamn

    davepamn Active Member Licensed User

    Code:
    Private arrSegmentControl(100As 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: Mar 19, 2015
  4. Brian Robinson

    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
    Code:
    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

    Code:
    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

    Code:
    ' 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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice