简体   繁体   English

隐藏按钮Obj-C

[英]Hiding a button Obj-C

I'm looking to hide a button on my i-phone app and then by clicking another button it will appear. 我想在我的iphone应用程序上隐藏一个按钮,然后通过单击另一个按钮将出现它。 I've managed to make the button disappear with a click but can't figure out the opposite. 我设法通过单击使按钮消失,但无法找出相反的方向。 I'm also new to Objective-C as you can probably tell so any tips on improving my code would be helpful. 您可能会告诉我,我对Objective-C还是陌生的,所以任何有关改进我的代码的技巧都将有所帮助。 Thanks! 谢谢!

.h : 。H :

@property(nonatomic,retain) IBOutlet UIButton* button1 ;


-(IBAction)buttonTouch:(id)sender ;

.m : .m:

@synthesize button1=_button1;

-(BOOL)hideOutlets {    
    _button1.hidden=TRUE;
}

-(void)buttonTouch:(id)sender {
    _button1.hidden =  !_button1.hidden;
}

Well to start from scratch, if you want to hide a button set its property hidden to YES, else if you want to make it reappear then set the property to NO. 从头开始,如果要隐藏按钮,请将其属性hidden为YES,否则,如果要使其重新出现,则将该属性设置为NO。

Example: 例:

button1.hidden=YES;
button1.hidden=NO;

Your code is basically correct 您的代码基本上是正确的

-(void)buttonTouch:(id)sender {
    _button1.hidden =  !_button1.hidden;
}

This code will hide your button when it's shown and show it when it's hidden. 此代码将在显示按钮时将其隐藏,而在隐藏时将其显示。 This should be correct. 这应该是正确的。

You are saying 你是说

then by clicking another button it will appear 然后单击另一个按钮,它将出现

Are you sure both buttons have the touch-up-inside event properly connected to this action? 您确定两个按钮都具有与此操作正确连接的内部触摸事件吗? I guess your problem will be that the buttonTouch: is not called when you touch the other button. 我想您的问题是,当您触摸另一个按钮时,不会调用buttonTouch:

 @synthesize button1=_button1;
    -(BOOL)hideOutlets {

    _button1.hidden=TRUE;

}

-(void)buttonTouch:(id)sender {

        _button1.hidden = FALSE; //Or "NO" or "0", it's a boolean 
}

In addition, its weird setting a button hidden with a BOOL . 此外,将按钮设置为BOOL隐藏起来很奇怪。 If you want to have them hidden on load, go put _button1.hidden = YES; 如果要在加载时隐藏它们,请放_button1.hidden = YES; if you want it to hide it only when you have it visible, use 如果希望仅在可见时将其隐藏,请使用

-(void)buttonTouch:(id)sender {
if(_button1.hidden == YES) 
    {
        _button1.hidden = NO; 
    }
else { _button1.hidden = YES; }
 }

I'll try to answer the question correctly as I understand it. 据我了解,我将尝试正确回答问题。

2 buttons, button1 and button2. 2个按钮,button1和button2。 Pressing button1 hides itself and shows button2. 按下button1隐藏自己并显示button2。 Pressing button2 hides itself and shows button1 again. 按下button2隐藏自身并再次显示button1。

-(IBAction)button1Pressed:(id)sender {
    // button1 can only be pressed when not hidden, so we can dispense with checks for hidden
    [button1 setHidden:YES];
    [button2 setHidden:NO]; // assuming this button was hidden at startup
}

-(IBAction)button2Pressed:(id)sender {
    // button2 can only be pressed when not hidden, so no need to check for hidden
    [button2 setHidden:YES];
    [button1 setHidden:NO];
}

This should allow you to flip back and forth between buttons having them hide/show opposite of each other. 这应该允许您在按钮之间来回切换,以使它们彼此隐藏/显示。

Two obvious problems with the code presented. 所提供的代码存在两个明显的问题。

1) Cocoa uses YES and NO for boolean values not TRUE and FALSE. 1)可可对布尔值(TRUE和FALSE)使用YES和NO。

2) You've declared a property, so you should use it in preference to the synthesised instance variable. 2)您已经声明了一个属性,因此应优先于综合实例变量使用它。

3) You're button touch method should return IBAction in the implementation as well as the interface. 3)您的按钮触摸方法应在实现以及界面中返回IBAction。

Don't know if that'll fix your problem, but it's the first step to fix up your code. 不知道这是否可以解决您的问题,但这是修正代码的第一步。

@synthesize button1=_button1;

-(BOOL)hideOutlets {

    self.button1.hidden=YES;

}

-(IBAction)buttonTouch:(id)sender {

    self.button1.hidden =  !self.button1.hidden;
}

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

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