简体   繁体   English

iOS / iPhone-单击另一个按钮时隐藏一个按钮

[英]iOS / iPhone- Hiding one button when another is clicked

I have a settings view in my app which has a couple of buttons (actually UISwitches). 我的应用程序中有一个设置视图,它有几个按钮(实际上是UISwitch)。 If the "off" setting on one of the switches is selected, I'd like to hide the second switch immediately. 如果选择其中一个开关上的“关闭”设置,我想立即隐藏第二个开关。 Can this be done? 可以这样做吗?

IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;

write the above 2 lines in your .h file and set the outlets with XIB. 在.h文件中写上面两行,并用XIB设置出口。

Now create a method called hideButton 现在创建一个名为hideButton的方法

-(IBAction)hideButton
{
       btn1.hidden = YES;
}

in XIB attach this method with btn2 . 在XIB中使用btn2附加此方法。 So now when you click on btn2 it will hide btn1 . 所以现在当你点击btn2它会隐藏btn1

Connect the two switches as outlets. 将两个开关连接为插座。 lets say switch1 & switch2. 让我们说switch1和switch2。

Connect this function to the valueChanged action. 将此函数连接到valueChanged操作。

- (IBAction)mySwitch1:(id)sender { 
    [switch2 setHidden:!(switch1.isOn)];
}

Now when switch1 is not on then switch2 will be hidden. 现在当switch1没有打开时,switch2将被隐藏。

Add a target to the first switch which on value change calls the instance of the second switch and hides it. 将目标添加到第一个交换机,该值在更改时调用第二个交换机的实例并将其隐藏。

Add the target: 添加目标:

    [switch1 addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventValueChanged];

Calls this method: 调用此方法:

- (void) switchToggled:(UISwitch*)switch {
 if ([switch isOn]) switch2.hidden = YES;
 else switch2.hidden = NO;
}

NJones if statement is more effective though. NJones if语句更有效。

Swift 4 斯威夫特4


Within your function do the following: 在您的函数中执行以下操作:

btn1.isHidden = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM