gpt4 book ai didi

ios - 面对 UISwitch 打开/关闭的问题

转载 作者:行者123 更新时间:2023-11-28 20:22:09 26 4
gpt4 key购买 nike

屏幕上有 6 个开关控件。一次只能启用一个开关。如果第 5 个开关打开,则一个标签和一个文本字段应该可见或者隐藏。

当 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];

}

最佳答案

当开关打开时,您隐藏标签 (.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.

}

关于ios - 面对 UISwitch 打开/关闭的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15484285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com