Search code examples
iosuiswitch

UISwitch is not working


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.


Solution

  • 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!!