I’m trying to change the value of a UISwitch based on whether you have enabled location services. I’m setting the target event in the ViewDidLoad method, but because you could close the app the load it back up from the background, I’m changing the value of UISwitch in the ViewDidAppear:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.cell2Switch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
...
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(![AppDelegate sharedAppDelegate].locationServicesEnabled && self.cell2Switch.on)
{
self.cell2Switch.userInteractionEnabled = NO;
[self.cell2Switch setOn:NO];
}
}
When I change the value, it triggers the target event I assigned in the ViewDidLoad.
Is there a way I can change the value of the UISwitch without it automatically triggering the event?
You would need to remove the target event.
If the
UISwitchserves no purpose other than to show you that something is enabled or disabled, then you do no need the target event.