Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以%i形式获取UISwitch的值_Ios_Objective C - Fatal编程技术网

Ios 以%i形式获取UISwitch的值

Ios 以%i形式获取UISwitch的值,ios,objective-c,Ios,Objective C,我有个无聊的小问题 我需要传递UISwitch设置的值,因为它是一个布尔值,所以我认为它将返回一个整数,因此在下面的代码中是%I 我已经看过很多UI,它们的值显示如下: if(nameOfSwitch.isOn) { NSLog(@"The switch is on"); } else { NSLog(@"The switch is off"); } 我需要布尔值,因为我需要: NSString *value = [[NSString alloc] initWithFormat:@"V

我有个无聊的小问题

我需要传递UISwitch设置的值,因为它是一个布尔值,所以我认为它将返回一个整数,因此在下面的代码中是%I

我已经看过很多UI,它们的值显示如下:

if(nameOfSwitch.isOn)
{
  NSLog(@"The switch is on");
}
else
{
  NSLog(@"The switch is off");
}
我需要布尔值,因为我需要:

NSString *value = [[NSString alloc] initWithFormat:@"Value of Switch %i" nameOfSwitch.on];
然而,这似乎总是返回打开-那么什么是正确的方式来做呢

我需要UISwitch的UITextField.text的等价物

您可以试试

NSString *value = [[NSString alloc] initWithFormat:@"Value of Switch: %i",nameOfSwitch.isOn];

顺便说一句,应该是这样的

if(nameOfSwitch.isOn)
{
  NSLog(@"The switch is on");
}
else
{
  NSLog(@"The switch is off");
}

编辑:如果您看到此开关处于打开状态,并且没有得到所需的1,那么您的问题可能是调用了错误的开关名称,或者它不是同一个实例(您在Interface Builder中有一个,并且您也通过代码初始化一个)

请尝试
[[NSNumber numberWithBOOL:switch.ON]intValue]%i
(或
%d
)的smgt对于
BOOL
值是合适的。对于
NO
您应该看到
0
,对于
YES
您应该看到
1
。你得到了什么?我刚刚得到1,但在开关关闭的情况下测试过它后,它仍然传递1而不是0。使用调试器并验证开关的状态。你的第二行工作:-)我猜它需要一个字符串作为占位符来显示。第一行将开关的值显示为整数,第二个根据开关的值显示我在那里写的文本。顺便说一句,当您想要获取值时,请尝试使用isON,因为这是正确的getter。
if(nameOfSwitch.isOn)
{
  NSLog(@"The switch is on");
}
else
{
  NSLog(@"The switch is off");
}