1

I have an app, where when the button is pressed, an action happens .. I can get the button to change when the action starts by using:

UIImage *changeImage = [UIImage imageNamed:@"stopGrey.png"];
[myButton setImage:changeImage forState:UIControlStateNormal];

After the action ends, I want to be able to change the button back.. I cannot figure out how to do this..

I've tried UIControlStateDisabled/Selected/Application.. I've logged to make sure the end of action is being received.

Thanks for any help..

ICL1901
  • 7,632
  • 14
  • 90
  • 138

2 Answers2

4

Ok, you need to do this:

.h file:

-(IBAction)ButtonPressed:(id)sender;
-(IBAction)ButtonReleased:(id)sender;

if you are using the graphical part join the touch down event to the method ButtonPressed and the touch up inside to ButtonReleased (right click the button for these options to appear). If you are using code to add the buttons use the method (in the viewdidload method of the .m file):

[button addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(ButtonReleased:) forControlEvents:UIControlEventTouchUpInside];

Then you're done. The method ButtonPressed will be called every time you touch the button and the ButtonReleased method will be called every time the you let go of the button inside of it. For your question, you can add the following code to the methods (.m file)

-(IBAction)ButtonPressed:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenPressed.png"] forState:UIControlStateNormal];
}
-(IBAction)ButtonReleased:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenReleased.png"] forState:UIControlStateNormal];
}

I am also guessing that you don't know how to link a button drawn in the graphical file to a pointer in the code. First you create a pointer in the .h file:

IBOutlet UIButton *button;

Then, you go to the graphical file and right click-drag the file owner image (on the left, has a orange-transparent cube as an image) to the button. Then, you select the option that has the name of the pointer. In this case, button.

And there! you're done!

rdelfin
  • 819
  • 2
  • 13
  • 31
  • Could you be a bit more specific. I do have another method that is called when the action ends. How do I tell this method to release the button? Thanks – ICL1901 Dec 16 '11 at 12:31
  • Also, preferably use the setBackgroundImage instead of setImage because the setImage blocks the text set with the setTitle method – rdelfin Dec 18 '11 at 05:40
3

Simply put something like this in viewDidLoad:

[myButton setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"Selected.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:@"Highlighted.png"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"Disabled.png"] forState:UIControlStateDisabled];

Then the button itself will handle the image changes. If you want the button to be visibly disabled while completing a background task simply set enabled to NO and it will show the disabled image.

You can test these setting by using an touchUpInside target like:

-(void)buttonPushed:(id)sender{
    [myButton setEnabled:NO];

    // Simulate pause before stuff is done
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [myButton setEnabled:YES];
    });
}
NJones
  • 27,139
  • 8
  • 70
  • 88
  • Thank you all. I figured that the recording could be so short, it ended before the code to change the button had a chance to execute. In summary, I got this built fine for recordings, but I gave up on trying to change the button for playing a short audio. – ICL1901 Dec 19 '11 at 03:56