mrEmpty
mrEmpty

Reputation: 841

iOS UIButton, setEnabled and button.hidden not working

I've tried all the answers I found on SO, so either I have a different problem or the heat got to my brain.

Anyway, I have a UIButton on a storyboard, it's linked to an IBAction called _cameraButtonPress (the UIButton is called _cameraButton). When the button is pressed I want to disable the button for a time, so I immediately call [sender setEnabled:NO]; and this works fine. However, in a different function which saves out the image, on successful save I use [_cameraButton.setEnabled:YES]; This does not work.

If I replace sender with _cameraButton in the function called by the IBAction, this also does not work. I have the UIButton linked with an IBOutlet.

I can post code, but it's quite a big project now, here are the relevant bits:

In the header...

IBOutlet UIButton *_cameraButton;

The IBAction...

- (IBAction) _cameraButtonPress:(id)sender {

[sender setEnabled:NO];  //stops button responding to touch events

Further on down in a different function:

} else {
    NSLog(@"colour image saved successfully");
    [_cameraButton setEnabled:YES];


}

Any ideas?

Thanks.

Upvotes: 0

Views: 4680

Answers (2)

Rob
Rob

Reputation: 437402

Paul's answer is the most logical problem.

As an aside, the underscore convention is used to differentiate between ivars and properties. Thus, you would have a property something like (for ARC):

@property (nonatomic, weak) IBOutlet UIButton *cameraButton;

And, if you needed the ivar, your @synthesize statement could be:

@synthesize cameraButton = _cameraButton;

This is probably less relevant with buttons than other properties, but good practice suggests underscores for ivars associated with declared properties, not for the properties themselves.

See Naming Properties and Data Types in Apple's Coding Guidelines for Cocoa.

Upvotes: 1

Paul Hunter
Paul Hunter

Reputation: 4463

Are you sure you have connected _cameraButton with the UIButton? Try logging _cameraButton to see if it holds a reference to a valid object.

NSLog(@"%@", _cameraButton);

Upvotes: 5

Related Questions