0

屏幕上有 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];

}
4

2 回答 2

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. 

}
于 2013-03-18T18:48:33.797 回答
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];

}
于 2013-03-18T18:44:55.893 回答