iOS Question [solved] CustomListView with multiple Switch buttons - getting their status

ThePuiu

Active Member
Licensed User
Longtime User
I have a Custom ListView control in which each panel contains a Switch button. I want to be able to scroll through all the buttons in the list and get their value. I tried using the following code, but I get an error message.
B4X:
Dim selectedValues As List
    
    For Each v As B4XView In pg.RootPanel.GetAllViewsRecursive
        Dim tag As String = v.Tag
        
        If tag.StartsWith("val") Then
            'each switch button Tag is like "val..."
            Dim but As Switch
            but.Initialize("")
            but = v
            If but.Value = True Then
                selectedValues.Add(tag)
            End If
        End If
    Next

Error message:

Error occurred on line: 153 (Grafice) (but = v)
Expected: UISwitch, object type: B4IPanelView
Stack Trace: (
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 1227168
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 135252
xBeam -[B4IObjectWrapper setObject:] + 276
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 1252752
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 7120
xBeam +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
xBeam -[B4IShell runVoidMethod] + 232
xBeam -[B4IShell raiseEventImpl:method:args::] + 1800
xBeam -[B4IShellBI raiseEvent:event:params:] + 1580
xBeam __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
libdispatch.dylib 3A5DB4E0-BC24-375D-897E-51E6CF7D6304 + 374288
libdispatch.dylib 3A5DB4E0-BC24-375D-897E-51E6CF7D6304 + 377220
libdispatch.dylib 3A5DB4E0-BC24-375D-897E-51E6CF7D6304 + 57808
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 693188
CoreFoundation DA838E75-6B30-360E-9661-C4800A7E1BF6 + 672696
CoreFoundation CFRunLoopRunSpecific + 464
GraphicsServices GSEventRunModal + 104
UIKitCore UIApplicationMain + 1936
xBeam main + 124
libdyld.dylib E1637502-BFCB-3BBC-B3E1-CCF78617E0E4 + 5216
)

What am I not doing well?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Big mistake:
B4X:
 Dim but As Switch
 but.Initialize("")
Never initialize an object before you assign a different object to the same variable. You are creating a switch object every iteration.
Based on the error message you set the tag on a panel and not the switch.

This code is from the B4A example:
B4X:
For i = 0 To clv2.GetSize - 1
   Dim p As B4XView = clv2.GetPanel(i)
   Dim chk As B4XView = p.GetView(2) 'the Switch is the third view in the item's layout.
   If chk.Checked Then
       checkedItems.Add(clv2.GetValue(i))
   End If
Next

You should use similar code.
 
Upvote 0
Top