我正在尝试让我的应用程序在UISwitch设置为“On”位置时运行一个数学公式,并在设置为“off”位置时运行另一个数学公式。
我试图像这样说出来:
如果开关=开,则[第一个公式]
如果开关=关,则[第二个公式]
我该怎么编码?
=============
编辑:
这就是我目前正在尝试编码的方式。
-(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];
}}
}
答案 0 :(得分:2)
鉴于:
@property (nonatomic) IBOutlet UISwitch *switch;
然后:
self.switch.on ? [self formula1] : [self formula2];
答案 1 :(得分:2)
您需要将这样的函数连接到uiswitch的值已更改为uievent。
-(IBAction) switchValueChanged:(UISwitch *) sender
{
if(sender.on)
{
...
}
else
{
...
}
}
答案 2 :(得分:1)
以下是在声明中使用uiswich开启或关闭的示例:
-(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];
}
}
答案 3 :(得分:0)
UISwitch有property named on,您可以使用它来检测开关是打开还是关闭。或者您是否在询问如何在实际翻转开关时编写的代码?