同时检查更多UISwitch上的状态

时间:2015-04-22 17:20:32

标签: objective-c uiswitch

我有5个UISwitch的simpel视图,我想确保只有4个可以同时关闭,如何检查更多的“如果”我试过这个它只是显示alertview加班我使用开关:

if (ishockeySwitch.state == NO | basketBallSwitch.state == NO | amrFootBallSwitch.state == NO | handBallSwitch.state == NO ) {



    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Sofabold" message:@"Det er ikke en god ide, at fravælge alle sportsgrene." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [av show];

   [ soccerSwitch setOn:YES animated:YES];
}else {
   // ........ do some stuff ...........
}

3 个答案:

答案 0 :(得分:0)

  

if(ishockeySwitch.state == NO | ishockeySwitch.state == NO | ishockeySwitch.state == NO | ishockeySwitch.state == NO)

您总是将同一个对象ishockeySwitch与NO进行比较。所有UISwitch都应该在不同的对象中声明。

if (ishockeySwitch1.state == NO || ishockeySwitch2.state == NO|| ishockeySwitch3.state == NO || ishockeySwitch4.state == NO ) 
{
   // DO SOME STUFF
}

答案 1 :(得分:0)

看来你问的是,只有在所有开关都关闭的情况下才能显示警报。换句话说,如果任何开关打开,请不要显示警报。假设这是正确的,最简单的逻辑如下:

if (ishockeySwitch.on || basketBallSwitch.on || amrFootBallSwitch.on || handBallSwitch.on) {
    // At least one is on, do some stuff
} else {
    // All are off, show the alert
}

另请注意使用||(逻辑OR)而不是|(按位OR)。

答案 2 :(得分:0)

感谢您帮助我们解决问题然后我只是让它工作,如果我检查开关是否如下所示:

 if ([ishockeySwitch isOn ]|| [ basketBallSwitch isOn ]|| [amrFootBallSwitch isOn] || [handBallSwitch isOn]) {
    // At least one is on, do some stuff



} else {
    // All are off, show the alert


}