xcode 禁用 UIButton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11517118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:55:09  来源:igfitidea点击:

disable a UIButton

objective-cxcodecocoa-touchuibutton

提问by Sahil Chaudhary

I have a round rect UIButtonwith an action method defined to it -(IBAction)btnclicked:(id)sender. Now I wanna create another method -(void)disableButtonwhich disables this button of mine. So I can call this function whenever I need. How can I use this mybtn.enabled = NO;into this function? What will this function look like?

我有一个圆形矩形,UIButton其中定义了一个动作方法-(IBAction)btnclicked:(id)sender。现在我想创建另一个方法-(void)disableButton来禁用我的这个按钮。所以我可以在需要的时候调用这个函数。我怎样才能mybtn.enabled = NO;在这个函数中使用它?这个函数会是什么样子?

回答by Rahul Vyas

Here is a simple solution

这是一个简单的解决方案

I assume mybtnwill be in your header file as a instance variable

我假设mybtn将在您的头文件中作为实例变量

Take another button and bind this below IBAction or you can call this function directly in the same class like this [self disableButton];

取另一个按钮并将其绑定在 IBAction 下方,或者您可以像这样直接在同一个类中调用此函数 [self disableButton];

-(IBAction)disableButton {
   //Disable  mybtn
    mybtn.enabled = NO;
}

回答by Zaraki

If you are using IB for button then create an IBOutlet for your button and map it with the button in InterfaceBuilder.

如果您将 IB 用于按钮,则为您的按钮创建一个 IBOutlet 并将其与 InterfaceBuilder 中的按钮映射。

IBOutlet UIBUTTON *mybtn;

Now in :

现在在:

-(void)disableButton{

mybtn.enabled = NO;

}

回答by Rinju Jain

.h

。H

 {
 IBOutlet UIBUTTON *mybtn;
 }
-(IBAction)btnclicked:(id)sender;
-(void)disableButton;  

.m

.m

 -(IBAction)btnclicked:(id)sender{ 
  [self disableButton];
  }
   -(void)disableButton {
    mybtn.enabled = NO;
  }