views:

165

answers:

2

I am new to Objective-C and Cocoa-Touch development. I have an ON/OFF Switch on a view that is supposed to disable/enable hide/show another button on the same view, but for some reason it doesn't work sometimes, this is the behavior:

1st click: Switch to OFF, Other Button hidden => OK
2nd click: Switch to ON, Nothing happens => WRONG
3rd click: Switch to OFF, Other button SHOWN => WRONG
4th click: Switch to ON, Nothing happens => WRONG
5th click: Switch to OFF, Other Button hidden => OK

This is the code:

MainViewController.h :

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
IBOutlet UIButton *resetButton;
}

@property (retain) UIButton *resetButton;

MainViewController.m :

@synthesize resetButton;

- (IBAction)switchTimer { 
resetButton.enabled = !resetButton.enabled;
resetButton.hidden = !resetButton.hidden;
}

What am I doing wrong? I appreciate your help.

+1  A: 

Well as long as your UISwitch has the Value Changed action targeted at your MainViewController, then it should work. I would suggest the following however.

// Add the (id)sender parameter to obtain the UISwitch (less coupled approach)
-(IBAction)onSwitchChange:(id)sender
{
    // If a control is hidden, disabling it is not required
    resetButton.hidden = ![sender on];
}
Nick Bedford
Nick has a good suggestion. However, you need to find out what was wrong with your code, since the code you showed us looked right, yet you were seeing an incorrect behavior. You may still have problems even with better code, like Nick's. I suspect maybe -onSwitchChange was being targeted by multiple actions. Good luck.
mahboudz
A: 

Thanks for the help! actually the problem was I had used the "Touch Up Inside" action, I changed to the "Value Changed" event. and it worked!

Thanks a lot!

Juan Salcedo