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

Discussion in 'iOS Questions' started by hatzisn, Jun 6, 2019.

  1. hatzisn

    hatzisn Active 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:

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

    Code:
    Sub btn_Click
        
    Log(Sender)
       
        
    If Sender Is Button Then
            
    Try
                
    Dim btn As Button
                btn = 
    Sender
                
    Dim sTag(2As 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: Jun 6, 2019
  2. hatzisn

    hatzisn Active Member Licensed User

    Seems that I 've got that working. It seems that if you add a panel and initialize it with:

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

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

    Code:
    pnl.Initialize("")
    Then the buttons work.
     
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