0

I've created a custom UIButton called SectionButton.

The button have 2 images, one for the Normal State and other for Selected and Highlighted States.

The button also have a text and when the button is pushed down, the TitleEdgesInset must be adjusted.

In the init method I add the methods:

[self addTarget:self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];

But the method 'buttonNormal:' is never called, so I can't adjust the titleEdgeInsets property.

#import "SectionButton.h"

@implementation SectionButton

- (id)initWithFrame: (CGRect)frame andTitle: (NSString*)title andbaseImageName:(NSString*)imageBaseName
{
if (self = [super initWithFrame: frame])
{
    // Create images for button
    UIImage* normalImage = [UIImage imageNamed: imageBaseName ];
    UIImage* downImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageBaseName]];
    // Set up button
    [self setTitle: [title uppercaseString] forState: UIControlStateNormal];    // Will be used for all states
    [self setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
    [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

    [self setBackgroundImage: normalImage forState: UIControlStateNormal];
    [self setBackgroundImage: downImage forState: UIControlStateHighlighted];
    [self setContentEdgeInsets:UIEdgeInsetsMake(54, 0, 0, 0)];

    [self addTarget: self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
    [self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];

}
return self;
}

- (void)buttonHighlighted: (id)sender
{
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,-16,0)];
NSLog(@"button selected");
}

- (void) buttonNormal: (id)sender {
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
NSLog(@"Button normal");
}    

}
@end
Jimmy
  • 873
  • 3
  • 12
  • 29

2 Answers2

3

Your are passing control state in place of ControlEvent,

[self addTarget:self action: @selector(buttonHighlighted:) forControlEvents: UIControlEventTouchDownInside];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpInside];
Dushyant Singh
  • 721
  • 4
  • 13
  • Yes, because I want to launch that methods when the button state change, not when the user taps in the button because in that case I have to add more targets to UIControlEventTouchCancel,UIControlEventTouchDragOutside... – Jimmy Dec 17 '12 at 11:50
  • Button state will only change when the user taps on it , how else you want to change the button state – Dushyant Singh Dec 17 '12 at 12:12
  • I want to catch the change of the button to the UIControlStateNormal. When the button is touched, the image change but the title don't change its titleEdgeInsets for a while, and that brokes the effect of 'pressed'. – Jimmy Dec 17 '12 at 12:25
  • When user touches the btn UIControlEventTouchDownInside get called and when it releases it UIControlEvenTouchUpInside get called , now you have to do you customization in these methods , I guess – Dushyant Singh Dec 17 '12 at 12:32
  • No because you have to consider other events like: UIControlEventTouchDragOutside. But none of these events acts at the same time that the image changes for UIControlStateNormal, and the event UIControlStateNormal never gets fired. – Jimmy Dec 17 '12 at 13:46
1

You need to call only 1 method to that button.

And in that method, you'll need to execute the code as per button's current state.

Hemang
  • 1,224
  • 9
  • 15
  • That doesn't works because the button restores 'Normal' state after 'buttonNormal:' si called. So I have the text with no adjustements in the titleEdgeInsets. – Jimmy Dec 17 '12 at 12:08
  • then once buttonNormal: method is called, change the state to highlighted or any other state whatever you want to be implemented. – Hemang Dec 17 '12 at 12:41