iOS Question How can I create a dynamic checkbox? Do I use the segment control

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

  1. davepamn

    davepamn Active Member Licensed User

    I need to create a dynamic checkbox control
     
  2. Brian Robinson

    Brian Robinson Active Member Licensed User

    iOS has no checkbox, so A SegmentedControl would probably suit what you want to do.

    Just need to create a List of items to display and set it using

    Code:
    Dim sc as SegmentedControl
        sc.Initialize(
    "sc")
        
    Dim items() As String = Array As String("A","B","C")
        sc.SetItems(items)
     
  3. davepamn

    davepamn Active Member Licensed User

    Here is the final code
    Code:
    sub Process_Globals
       
    Private listSegmentControl As List
    end sub

    Sub CreateSegmentControl

                    
    Dim sc As SegmentedControl
                    
    Dim oSCItems() As String=Array As String("Yes","No")
                    sc.Initialize(
    "seq"&iSegmentControlCounter)
                    sc.Tag=sTestId
                    sc.SetItems(oSCItems)
                    
    If sTestIdValue="True" Then
                        sc.SelectedIndex=
    0
                    
    Else
                        sc.SelectedIndex=
    1
                    
    End If

    ScrollViewInput.Panel.AddView(sc,
    200dip,iRowOffset,50%x,40dip)
                    iRowOffset=iRowOffset+
    80
                    listSegmentControl.Add(sc)

    end sub

    sub cmdSave_Click

    Dim oSegmentControl As SegmentedControl
    For i=0 To listSegmentControl.Size-1
       oSegmentControl=listSegmentControl.Get(i)
      sTestId=oSegmentControl.Tag
      
    If oSegmentControl.SelectedIndex=0 Then
        sTestValue=
    "Y"
      
    Else
        sTestValue=
    "N"
      
    End If
    Next
    end sub
     
    Last edited: Mar 25, 2015
  4. Brian Robinson

    Brian Robinson Active Member Licensed User

    I think for this instance of simply Yes/No a Switch view is better suited:
    1. There are only two answers
    2. The answers are Yes/No

    If the two outcomes were Water/Milk then it makes sense to use a Segmented Control because you need the control to represent those two outcomes in their text.

    Switch would be used for Yes/No, True/False, On/Off. The do look nicer on screen in those situations too.

    Just my opinion.

    Cheers
    Brian
     
    Last edited: Mar 25, 2015
  5. davepamn

    davepamn Active Member Licensed User

    I will look at the switch view. I have the segment control working correctly
     
  6. Brian Robinson

    Brian Robinson Active Member Licensed User

    Hey Dave,

    If you do decide to go with the switches, there are hardly any code changes involved as what you have already done is what is needed. Just need to swap out the controls.

    * Note I don't have access to my compiler to see if this code actually runs, but will fix later when I do.

    Similar to this:
    Code:
    Sub Process_Globals
        
    ' .....
        Private listSwitchControl As List
    End Sub

    Private Sub CreateSwitchControl
        
    Dim sw As Switch
        sw.Initialize(
    "sw"&iSwitchControlCounter)
        sw.Tag = sTestId 
    ' I assume sTestId is defined elsewhere
        If sTestIdValue = "True" Then ' I assume sTestIdValue is defined elsewhere
            sw.Value = True
        
    Else
            sw.Value = 
    False
        
    End If
       
        ScrollViewInput.Panel.AddView(sw,
    200dip,iRowOffset,50%x,40dip' I assume iRowOffset is defined elsewhere
        iRowOffset=iRowOffset+80
        listSwitchControl.Add(sw)

    End Sub

    Private Sub cmdSave_Click
        
    For Each sw As Switch In listSwitchControl
            
    If sw.Value = True Then
                sTestValue=
    "Y" ' I assume this is a global somewhere
            Else
                sTestValue=
    "N"
            
    End If
        
    Next
    End Sub
     
  7. davepamn

    davepamn Active Member Licensed User

    I will try it
     
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