iOS Simple Button driving me mad

seepel

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

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,580
4,020
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.
 

xStep

macrumors 68020
Jan 28, 2003
2,006
99
Less lost in L.A.
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];
 

seepel

macrumors 6502
Original poster
Dec 22, 2009
471
0
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!