同时检查更多UISwitch的状态



我有一个带有 5 个 UISwitch 的简单视图,我想确保只有 4 个可以同时设置为关闭,我如何在一个"如果"中检查更多"我尝试了这个,它只显示警报视图加班我使用开关:

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 ...........
}
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
}

谢谢你的帮助,我不得不 || 然后我只有在检查开关是否打开时才让它工作,如下所示:

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

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

}

看来您要问的是,仅当所有开关都关闭时,如何显示警报。换句话说,如果任何开关打开,请不要显示警报。假设这是正确的,最简单的逻辑如下:

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)。

最新更新