Simple Button driving me mad

Discussion in 'iOS Programming' started by seepel, Jan 4, 2012.

  1. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #1
    I've spent way to long on the trying to figure out why this button won't call its selector. Does anyone see anything obvious that I'm doing wrong? I add the button to my view

    Code:
    
    - (id)initWithFrame:(CGRect)frame {
    	if((self = [super initWithFrame:frame])) {
                    [self addSubview:self.backButton];
    	}
    	return self;
    }
    
    - (void)awakeFromNib {
        [self addSubview:self.backButton];
    }
    
    - (UIButton *)backButton {
        if(backButton_ == nil && self.presentingViewController != nil) {
            backButton_ = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
            [backButton_ setTitle:@"Back" forState:UIControlStateNormal];
            [backButton_ addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [backButton_ setUserInteractionEnabled:YES];
            backButton_.frame = CGRectMake(self.bounds.size.width-20-50, 20, 50, 44);
        }
        return backButton_;
    }
    
    - (void)backButtonPressed:(id)sender {
        NSLog(@"pressed");
        [self.presentingViewController dismissModalViewControllerAnimated:YES];
    }
    
    
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I don't see anything obvious that you're doing incorrectly...

    perhaps you should place a breakpoint on the line

    Code:
    [backButton_ addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    just to verify that this line is, in fact, being reached.

    Your code strikes me as a little convoluted (if you're using a nib, which I assume you are given "awakeFromNib", why not just set up your button inside your nib?) but I can't see why it wouldn't work.
     
  3. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    Is this view created from a nib file? Is a button in that view assigned to backButton_? If so, then you need to do the action assignment there, or remove that button or connection.

    In your backButton method I'd use NSLog to inspect backButton_ and self.presentingViewController. Or, just place an NSLog in the execution path to see if the path is taken. That block worked in a test I did in that my new button called the selector.

    Also, either perform a release on backButton_ after adding it as a subview don't do the retain on backButton_. Adding the button as a subview causes a retrain to occur.

    Oh, lastly in your backButtonPressed method try the following line instead.
    Code:
    [self dismissModalViewControllerAnimated: YES];
     
  4. seepel thread starter macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #4
    I did inspect the button, and it was all fine. As it turns out I just needed to take a break from it, I had another view whose frame was overlapping the button so it was intercepting the touches. Thanks for the ideas though!
     

Share This Page