viewWithTag keeps returning the 'parent'

Discussion in 'iOS Programming' started by DennisBlah, Jul 14, 2014.

  1. DennisBlah, Jul 14, 2014
    Last edited: Jul 14, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I really need some quick help with this.
    I'm searching on a button based on the tag.
    As the buttons in the UIView buttons are images, I want to change them after they are pressed.

    Code:
    -(IBAction)buttonPressed:(id)sender {
    ...
    ...
       UIButton *button = (UIButton *)[self.buttons viewWithTag: [sender tag]];
       button.hidden = YES;
    }
    
    However.. the UIButton keep returning the UIView buttons. And my UIView buttons get hidden when I press the first button (tag 0)
    Whilst I'm searching the UIButton in UIView buttons based on the tag.
    Anyone has any explanation and solution for this ?

    For all buttons it's working except for my 'home' button. Then the whole view gets hidden :s
    Even when I set tag of the buttons UIView to something like 9999 and with a isMemberOfClass checksum I never get to the first button (tag 0)

    p.s. the button.hidden is for testing
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    The default value of tag is 0. Therefore, all unassigned tags are 0. You most certainly have overlap. Don't use 0 as a "special case".
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    The uiview tag is 9999
    But, oh wait I think that the uiimageview I use for background is 0 as well

    I use 0 so I can throw the tag into objectatindex of my buttons array
     
  4. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #4
    Try this

    Code:
    -(IBAction)buttonPressed:(id)sender {
    
       UIButton *button = (UIButton *) sender;
       button.hidden = YES;
       NSLog(@"tag: %i", button.tag);
    }
    
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    This does work. However it will remove all the buttons with this tag.
    (I got the same tags in another 'sidebar' which got the same functions).

    I only want to hide the button with this [sender tag] that is inside the UIView buttons. That's why I was playing around with the viewWithTag
     
  6. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #6
    Have you tried it?
    the 'sender' above is the button that was pressed. That is the only button that will be removed. As you can see the tag of the pressed button will also be logged. The code I posted won't remove anything else.

    If you are still playing with tags, remember the default tag is 0.
     
  7. DennisBlah, Jul 17, 2014
    Last edited: Jul 18, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    I did try and it does work, but I really need to work with the tag. However it's removing all buttons with the tag
    When I press like... gallery in the sidebar, the favorite icon of gallery must change, this one has same tag as the button the sidebar

    This buttonbar is like.. Favorites of all the buttons.
    What Im trying to say is, the same buttons (but only text) and same tag is in another view on the viewcontroller. Like a sidebar which slides in and out.
    Favorites are also shown in a bottombar that is dynamic in size and the buttons are shown as icon which I want to change color when it's currently viewing that module or page
    And only the favorites must change thats why I am trying with viewWithTag, also when I press the button in the 'sidebar' the favorites must change.
     

Share This Page