Reputation: 15
I am trying to get my app to run one mathematical formula when UISwitch is set to "On" position, and another when it's set to "off" position.
I'm trying to lay it out like this:
if switch = on, then [first formula]
if switch = off, then [second formula]
how would I code this?
=============
EDIT:
This is how I am currently trying to code it.
-(IBAction)switchValueChanged:(UISwitch *)sender
{
if(sender.on)
{
-(float)conver2inches: (NSString *)mmeters {
return [mmeters floatValue]/25.4f;
}
-(IBAction)calculate:(id)sender{
float answer = [self conver2inches:entry.text];
output.text=[NSString stringWithFormat:@"%f",answer];}}
else{
-(float)conver2mmeters:(NSString *)inches {
return [inches floatValue]*25.4f;
}
-(IBAction)calculate:(id)sender{
float answer = [self conver2mmeters:entry.text];
output.text=[NSString stringWithFormat:@"%f",answer];
}}
}
Upvotes: 1
Views: 2670
Reputation: 6529
Here is a example of using uiswich on or off in a statement:
-(IBAction)onOffSwitch:(id)sender{
if(onOffSwitch.on) {
// lights on
[onOffLabel setText:@"Lights Currently On"];
onOffLabel.textColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:219.0/255.0 blue:52.0/255.0 alpha:1.0];
}
else {
// lights off
self.view.backgroundColor = [UIColor blackColor];
[onOffLabel setText:@"Lights Currently Off"];
onOffLabel.textColor = [UIColor whiteColor];
}
}
Upvotes: 1
Reputation: 2538
You need to hook up a function like this to uiswitch's value changed uievent.
-(IBAction) switchValueChanged:(UISwitch *) sender
{
if(sender.on)
{
...
}
else
{
...
}
}
Upvotes: 1
Reputation: 11444
Given:
@property (nonatomic) IBOutlet UISwitch *switch;
Then:
self.switch.on ? [self formula1] : [self formula2];
Upvotes: 2
Reputation: 78842
UISwitch has a property named on which you can use to detect if the switch is on or off. Or are you asking how to write code that acts when the switch is actually flipped?
Upvotes: 0