Reputation: 27
Every time I switch the UISwitch to either on or off in the simulator, it still returns false even when one of the switches is on. I even tried adding
self.switch1.on = true;
inside the if statement of checkSwitch1Changed.
This is my code in .m file.
- (IBAction)checkSwitch1Changed:(id)sender {
if (self.switch1.on)
{
self.switch2.on = NO;
self.switch3.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch2Changed:(id)sender {
if (self.switch2.on)
{
self.switch1.on = NO;
self.switch3.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch3Changed:(id)sender {
if (self.switch3.on)
{
self.switch1.on = NO;
self.switch2.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch4Changed:(id)sender {
if (self.switch4.on)
{
self.switch1.on = NO;
self.switch2.on = NO;
self.switch3.on = NO;
}
...
}
This is my code in .h
@property (weak, nonatomic) IBOutlet UISwitch *switch1;
@property (weak, nonatomic) IBOutlet UISwitch *switch2;
- (IBAction)checkSwitch1Changed:(id)sender;
- (IBAction)checkSwitch2Changed:(id)sender;
//********
@property (weak, nonatomic) IBOutlet UISwitch *switch3;
@property (weak, nonatomic) IBOutlet UISwitch *switch4;
- (IBAction)checkSwitch3Changed:(id)sender;
- (IBAction)checkSwitch4Changed:(id)sender;
This actually works and sends back true if I send the data using that view controller. But I am trying to access this data from another view controller and it sends back false. I used this code to access the data from the other view controller.
//import data from SetupController'
SetupController *setupController = [[SetupController alloc] initWithNibName:@"SetupController" bundle:nil];
...
Firebase *postRef = [savedRef childByAppendingPath: @"Switches"];
NSDictionary *post1 = @{
@"Check1": @(setupController.self.switch1.on),
Upvotes: 0
Views: 1196
Reputation: 901
Did you try checking for the isOn
property, instead of on
?
if (self.switch1.on)
becomes
if (self.switch1.isOn)
Also check that the IBAction associated with the switch is a
ValueChanged
event, and that the reference outlets are properly linked.
Upvotes: 1
Reputation: 4105
You'll need to also 'set' the switch to on, rather than listening to see if it's on (without setting first, if that makes sense).
I'm sure there's a much more elegant way of doing this (it's a but long winded here), but you should get the idea and this method should work if you hook ALL the switches up to the one IBAction
and changed the sender
on the method from id
to UISwitch
.
(of course you don't need to use just the one IBAction, though it makes more sense if you do, as opposed to 4 actions all wanting to do the same thing)
- (IBAction)switchPressed:(UISwitch *)sender {
if (sender==self.switch1) {
self.switch1.on = YES;
self.switch2.on = NO;
self.switch3.on = NO;
self.switch4.on = NO;
}
if (sender==self.switch2) {
self.switch1.on = NO;
self.switch2.on = YES;
self.switch3.on = NO;
self.switch4.on = NO;
}
if (sender==self.switch3) {
self.switch1.on = NO;
self.switch2.on = NO;
self.switch3.on = YES;
self.switch4.on = NO;
}
if (sender==self.switch4) {
self.switch1.on = NO;
self.switch2.on = NO;
self.switch3.on = NO;
self.switch4.on = YES;
}
}
Upvotes: 0