屏幕上有6个开关控件。有一次只应启用一个开关。如果第五个开关打开,则应该可以看到一个标签和一个文本字段,或者它应该被隐藏。
我正面临问题,当5开关从关闭变为开启并再次变为关闭时。标签和文本字段应该是隐藏的,但不符合要求。
- (void)viewDidLoad
{
[super viewDidLoad];
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
-(IBAction)switchChange:(id)sender
{
if(sw_switch1.isOn)
{
//self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch2.isOn)
{
self.sw_switch1.on = NO;
//self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch3.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
//self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch4.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
//self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch5.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
//self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
else if(sw_switch6.isOn)
{
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
//self.sw_switch6.on = NO;
}
[self viewWillAppear:NO];
}
答案 0 :(得分:2)
当切换开关打开时,隐藏标签(.hidden = YES)。但是当其他人关闭时,你不会取消隐藏(.hidden = NO)。无论如何,你的代码有点不合逻辑。
我建议:
-(IBAction)switchChange:(id)sender
{
UISwitch *theSwitch = (UISwitch *) sender;
BOOL switchState = theSwitch.on; //this method is called when changed to on or off.
self.sw_switch1.on = NO; // force all switches off
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
theSwitch.on = switchState; // recover the state set by the user.
//now hide or unhide the labels depending on the state of switch 5.
lbl_desc.hidden = self.sw_switch5.on;
txt_desc.hidden = self.sw_switch5.on;
// [self viewWillAppear:NO]; // What the h.... is this? You are not supposed to call that method. Use self.view.setNeedsDisplay or so instead.
[self.view setNeedsDisplay]; // This may not be reqired but it forces the view to re-display itself.
}
答案 1 :(得分:1)
你可以试试这个
if(sw_switch5.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
//self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
}
else
{
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
我的建议是你也应该为每个开关控件使用标签值。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
sw_switch1.tag = 101;
sw_switch2.tag = 102;
sw_switch3.tag = 103;
sw_switch4.tag = 104;
sw_switch5.tag = 105;
sw_switch6.tag = 106;
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
修改:使用此if((theSwitch.tag == 105) && self.sw_switch5.on)
之类的标记值。
现在您可以尝试下面的优化代码
-(IBAction)switchChange:(id)sender
{
UISwitch *theSwitch = (UISwitch *) sender;
BOOL switchState = theSwitch.on; //this method is called when changed to on or off.
self.sw_switch1.on = NO; // force all switches off
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;// force the label and textfield should be hidden
txt_desc.hidden = YES;
theSwitch.on = switchState; // recover the state set by the user, so at a time only one switch should be on
//now hide or unhide the labels depending on the state of switch 5 only
if((theSwitch.tag == 105) && self.sw_switch5.on){
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
}
else{
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
[self.view setNeedsDisplay];
}