First my .h file:
@interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
IBOutlet UISwitch *gravaSwitch;
...
}
@property (retain, nonatomic) UISwitch *gravaSwitch;
...
@end
My viewDidload
in .m file (it works):
...
// SET SWITCH BUTTON STATE
keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"TrafficApp" accessGroup:nil];
if ( [[keychain objectForKey:(id)kSecAttrAccount] isEqualToString:@"" ] )
[self.gravaSwitch setOn:FALSE];
else [self.gravaSwitch setOn:TRUE];
...
But my switchChanged
doesn't work and I don't know why. On IB everything is right connected, it enters in this method but gravaSwitch
is always null.
- (IBAction)switchChanged:(id)sender
{
if ( self.gravaSwitch.on )
{
NSLog(@"IF");
[self.gravaSwitch setOn:FALSE animated:YES];
}
else
{
NSLog(@"ELSE");
[self.gravaSwitch setOn:TRUE animated:YES];
}
}
Regards.
I think the error is the following:
@interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
UISwitch *gravaSwitch;
...
}
@property (retain, nonatomic) IBOutlet UISwitch *gravaSwitch;
...
@end
IBOutlet placeholder has to be inserted into @property. Change your code and try to connect your outlet again.
Edit:
Try to create your UISwitch programatically. Change your @property
as the following:
@property (retain, nonatomic) UISwitch *gravaSwitch;
Leave @synthesize
as is. Then in your viewDidLoad
method add your UISwitch (by default on
property is FALSE):
// Width and height are set to zero but they take the default dimension
UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
// add target and action
mySwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
self.gravaSwitch = yourSwitch;
// don't forget because gravaSwitch has already a retain policy
[release yourSwitch];
// add the switch to the view
[self.view addSubview:self.gravaSwitch];
Within the same controller, but outside the viewDidLoad
method, add the following method:
- (void)yourCustomAction:(id)sender
{
if ( self.gravaSwitch.on )
{
NSLog(@"IF");
[self.gravaSwitch setOn:FALSE animated:YES];
}
else
{
NSLog(@"ELSE");
[self.gravaSwitch setOn:TRUE animated:YES];
}
}
- (void)dealloc
{
self.gravaSwitch = nil; // remember to dealloc the switch!!
[super dealloc]
}
You could call self.gravaSwitch = nil;
also in viewDidUnload
method.
As an alternative you can set gravaSwicth to assign policy as follow. In this case you haven't to call self.gravaSwitch = nil;
both in dealloc
and/or viewDidUnload
.
@property (assign, nonatomic) UISwitch *gravaSwitch;
Hope it helps.
Edit 2:
This code works for me. This is the implementation (.m file) for MyViewController.
@synthesize gravaSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
[yourSwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
self.gravaSwitch = yourSwitch;
[yourSwitch release];
[self.view addSubview:self.gravaSwitch];
}
- (void)yourCustomAction:(id)sender
{
if(self.gravaSwitch.on)
{
NSLog(@"on");
}
else
{
NSLog(@"off");
}
}
where gravaSwicth is declared within MyViewController.h as follows:
@interface MyViewController : UIViewController
{
UISwitch *gravaSwitch;
...
}
@property (retain, nonatomic) UISwitch *gravaSwitch;
...
@end
rembember to call self.gravaSwicth = nil
in dealloc in MyViewController.m!!
-(IBAction)alarmSwitchToggled:(id)sender
{
if ([switchAlarm isOn]) {
NSLog(@"switch is ON");
}
else
{
NSLog(@"switch is OFF");
}
}
Give proper connections in nib file and dont forget to make it as valuechanged
instead of touchupinside
FWIW I had this problem when my UISwitch was in a UITableViewCell, and I put it at the top of the list of subviews in Interface Builder (and therefore beneath the other views in the cell when running). It worked in a develop build but not a production build for who knows what reason. In prod, I would click on the UISwitch and the whole row would blink ask if selected.
So I moved it to the bottom of the list (and therefore above the other views in the cell when running) and then I could click on the UISwitch.