4

I'm creating buttons programmatically and I then want to add functionality whereby when they are tapped on/pressed, they stay highlighted unless tapped on again. What I'm doing now is creating the buttons and then trying to add an IBAction. However, the issue is I have my button creation in a method and then I'm not sure how to reference the button in my IBAction. Here's my code:

UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
  [testButn setFrame:CGRectMake(0, 135, 40, 38)];
  [testButn setImage:[UIImage imageNamed:@"test_butn_un.png"] forState:UIControlStateNormal];
  [testButn setImage:[UIImage imageNamed:@"test_butn_pressed.png"]   forState:UIControlStateHighlighted];
[testButn addTarget:self action:@selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn

-(IBAction)staypressed:(id)sender{

//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
Cœur
  • 37,241
  • 25
  • 195
  • 267
David West
  • 552
  • 2
  • 9
  • 21
  • UIButton *theButton = (UIButton*)sender; theButton.selected = true; .selected may disable the button so may have just change the style. – BooRanger Jan 31 '13 at 16:22
  • Still not working. This is what I have now: -(IBAction)stayPressed:(id)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; } – David West Jan 31 '13 at 16:42

3 Answers3

9

The sender is testButn. You should change stayPressed's argument type from (id) to (UIButton *)

Unless an action method is connected to several different classes of objects, it's best to replace the id with whatever object class you're using. This can be useful if you're hooking things up in IB, since it won't let you hook it to the wrong kind of object.

The fact that it's not working isn't because it doesn't recognize your button. Your approach is wrong. I think you need to have an action connected to touch down, and maybe set the selected state to YES. In your button definition, you need to set an imageForState: for the selected state. The way you're doing it now, that method isn't called until touch up.

Something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
    [testButn setFrame:CGRectMake(0, 135, 40, 38)];
    [testButn setImage:[UIImage imageNamed:@"New_PICT0019.jpg"] forState:UIControlStateNormal];
    [testButn setImage:[UIImage imageNamed:@"New_PICT0002.jpg"]   forState:UIControlStateSelected];
    [testButn addTarget:self action:@selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:testButn];
}

-(void)stayPressed:(UIButton *) sender {
    if (sender.selected == YES) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Hmm, this is what I have at the moment and the button is still not staying highlighted. (IBAction)stayPressed:(UIButton *)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; } – David West Jan 31 '13 at 16:45
  • if you see my original code, I have an image for UIControlStateHighlighted. My action is connected to touchupinside. When I tap on the button it is highlighted but it doesnt stay highlighted. – David West Jan 31 '13 at 16:52
  • @DavidWest, you need to use the selected state, not highlighted. See my edit. By using touchDown, I can still connect the button to some other action on touch up inside. – rdelmar Jan 31 '13 at 16:56
  • Thanks, yeah my issue was using highlighted state instead of selected. – David West Jan 31 '13 at 17:09
2

You need to cast sender to UIButton.

- (IBAction)staypressed:(id)sender
{
    UIButton *theButton = (UIButton*)sender;

    //do something to theButton
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Still not working. This is what I have now: -(IBAction)stayPressed:(id)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; } – David West Jan 31 '13 at 16:40
  • ok, but what isn't working? Is the function being run at all? or are you getting an error? – Fogmeister Jan 31 '13 at 17:02
2
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
  [testButn setFrame:CGRectMake(0, 135, 40, 38)];
  [testButn setImage:[UIImage imageNamed:@"test_butn_un.png"] forState:UIControlStateNormal];
  [testButn setImage:[UIImage imageNamed:@"test_butn_pressed.png"]   forState:UIControlStateHighlighted];
  [testButn addTarget:self action:@selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
  testButn.tag = 1;
  [self.contentview addSubview:testButn

-(IBAction)staypressed:(id)sender
 {
     if ([sender tag]==1)
     {
         somecodes...
     }
 }
Yildizoglu
  • 21
  • 1
  • 2