Programaticly add buttons with an action?

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

  1. DennisBlah macrumors 6502

    DennisBlah

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

    I'm facing a new problem with adding my buttons.
    Now I'm adding buttons to the bottom bar. They show up, with the image, but...
    the action is never being called.

    This is proberbly a simple fault so hopefully someone can help me out here

    Code:
    for(int a=0;........
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                     NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString *path = [documentsDirectory stringByAppendingPathComponent:
                                  [NSString stringWithFormat: @"%@/favorites/%@", theSub, [currentFavoriteImgs objectAtIndex: a]]];
                UIImage *favoImage = [UIImage imageWithContentsOfFile: path];
                
                UIButton *favoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [favoButton setTag: a];
                [favoButton setTitle: @"" forState:UIControlStateNormal];
                favoButton.frame = CGRectMake(x, y, size, size);
                [favoButton setBackgroundColor: [UIColor clearColor]];
                [favoButton setBackgroundImage: favoImage forState:UIControlStateNormal];
                [favoButton setBackgroundImage: favoImage forState:UIControlStateHighlighted];
                [favoButton addTarget:self action:@selector(chosenBtnAction:) forControlEvents: UIControlEventTouchUpInside];
                
                [bottombar addSubview:favoButton];
    
    I'm really struggling for a few hours now :mad:
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What does your chosenBtnAction: method look like?
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Like this:
    Code:
    -(IBAction)chosenBtnAction:(id)sender {
        [self closeButtons: @""]; //Some function to close buttons animated
        NSLog(@"BLAH PRESSED BUTTON!");
        
        NSUInteger index = [@[@"page",@"gallery",@"lostfound", @"map", @"calendar", @"events"] indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            return [obj isEqualToString:[currentModules objectAtIndex: [sender tag]]];
        }];
        NSString *theNextView = nil;
        
        switch(index) {
    
    It looks like I can't press these buttons... Because the image is not getting highlighted or anything

    (I'm using the same function in my sidebar, and that works fine)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Well, you're using the same image for both normal and highlighted states, so that probably doesn't help:

     
  5. DennisBlah, Jul 13, 2014
    Last edited: Jul 13, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    You are right about that one ;-)
    That's why I have quoted it out in my project, see for any difference, but it doesn't make any..

    I don't even get my
    NSLog(@"BLAH PRESSED BUTTON!");
    Even if I quote out the [self closeButtons: @""]

    When I put a button manually with the a tag for an .. test. It does work properly, but the buttonbar must be dynamic :)

    For now I will workaround it. As the 'favoButtons' will be a max of 5, I will put 5 buttons in place, and change the image programaticly...

    Still I'm hoping that someone can help me out of this, because its not funny! :(
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Why are you not using UIBarButtonItem for your button bar?
     
  7. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    Because its a normal uiview
    Im fading in and out and its dynamic in sizes. The buttons will be dynamic in sizes as well
     

Share This Page