iOS Question [SOLVED] Dynamically created buttons in dynamically created panel problem

hatzisn

Well-Known Member
Licensed User
Hi everyone,

I do not know if anyone has faced that before as I didn't find something similar in the search. I dynamically create buttons and I assign them with a tag and an image with the following code I found in the forum:

B4X:
'state: 0 - normal, 1 - highlighted, 2 - disabled
Sub SetBackgroundImage(b As Button, bmp As Bitmap, state As Int)
    Dim no As NativeObject = b
    no.RunMethod("setBackgroundImage:forState:", Array(bmp, state))
End Sub
The tag is in the form (IDFromDB_Action where action is Add, Minus, Information).

The buttons are added in a panel which itself is added dynamically in the panel of a scrollview.
The code that I use for the Buttons Click event is the following:

B4X:
Sub btn_Click
    Log(Sender)
   
    If Sender Is Button Then
        Try
            Dim btn As Button
            btn = Sender
            Dim sTag(2) As String
            sTag = Regex.Split("_", btn.Tag)
            LogColor(btn.Tag, Colors.Red)
            Select Case sTag(1)
                Case "Add"
                        'Do something               
                Case "Minus"
                        'Do something               
                Case "Information"
                        'Do something

            End Select
            Return
        Catch
            'Log(LastException)
   
        End Try
    Else
        If Sender Is ImageView Then
            'The image is clicked perfectly every time
        End If
    End If
       
End Sub
The "Funny" but not so funny thing is that when I click on a button I get in the Log(Sender) the following clicking on the same button several times. As you will see clicking in the same area it detects clicking alternatively and by phone's "will" the button or the panel also with different CALayer values.

<B4IPanelView: 0x10ed43bd0; frame = (0 67; 266 67.2); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x283ae8f60>; layer = <CALayer: 0x2834b0ea0>>
<UIButton: 0x10ed485e0; frame = (222.4 30.6417; 33.6 33.6); opaque = NO; gestureRecognizers = <NSArray: 0x283aec1b0>; layer = <CALayer: 0x2834b11c0>>
2_Information
<UIButton: 0x10ed485e0; frame = (222.4 30.6417; 33.6 33.6); opaque = NO; gestureRecognizers = <NSArray: 0x283aec1b0>; layer = <CALayer: 0x2834b11c0>>
2_Information
<B4IPanelView: 0x10ed43bd0; frame = (0 67; 266 67.2); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x283ae8f60>; layer = <CALayer: 0x2834b0ea0>>
<UIButton: 0x10ed485e0; frame = (222.4 30.6417; 33.6 33.6); opaque = NO; gestureRecognizers = <NSArray: 0x283aec1b0>; layer = <CALayer: 0x2834b11c0>>
2_Information
<UIButton: 0x10ed485e0; frame = (222.4 30.6417; 33.6 33.6); opaque = NO; gestureRecognizers = <NSArray: 0x283aec1b0>; layer = <CALayer: 0x2834b11c0>>
2_Information
<B4IPanelView: 0x10ed43bd0; frame = (0 67; 266 67.2); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x283ae8f60>; layer = <CALayer: 0x2834b0ea0>>
<B4IPanelView: 0x10ed43bd0; frame = (0 67; 266 67.2); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x283ae8f60>; layer = <CALayer: 0x2834b0ea0>>
<B4IPanelView: 0x10ed43bd0; frame = (0 67; 266 67.2); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x283ae8f60>; layer = <CALayer: 0x2834b0ea0>>


I do not know what I have done wrong.

Any help will be highly appreciated.
 
Last edited:

hatzisn

Well-Known Member
Licensed User
Seems that I 've got that working. It seems that if you add a panel and initialize it with:

B4X:
pnl.Initialize("btn")
Then if you add a button to it and intitialize it again with:

B4X:
btnAdd.Initialize("btn")
Then when you click on the button it gets totally confused and it does not know what to choose but if you initialize the panel with:

B4X:
pnl.Initialize("")
Then the buttons work.
 
Top