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:
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
@propertyas the following:Leave
@synthesizeas is. Then in yourviewDidLoadmethod add your UISwitch (by defaultonproperty is FALSE):Within the same controller, but outside the
viewDidLoadmethod, add the following method:You could call
self.gravaSwitch = nil;also inviewDidUnloadmethod.As an alternative you can set gravaSwicth to assign policy as follow. In this case you haven’t to call
self.gravaSwitch = nil;both indeallocand/orviewDidUnload.Hope it helps.
Edit 2:
This code works for me. This is the implementation (.m file) for MyViewController.
where gravaSwicth is declared within MyViewController.h as follows:
rembember to call
self.gravaSwicth = nilin dealloc in MyViewController.m!!