Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seepel

macrumors 6502
Original poster
Dec 22, 2009
471
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];
}
 
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.
 
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];
 
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];

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.