1

我对 iPhone 开发非常陌生。我正在尝试禁用已经存在的按钮,但实际上无法获得指向视图中特定元素的指针。例如,我在 viewController 标头中有以下内容

- (IBAction)one:(id)sender;

并且实现是

- (IBAction)one:(id)sender {

}

这只是事件处理程序。但是,我需要在视图打开时禁用该按钮,并且我对如何获取对事件处理程序之外的元素的引用有点迷茫。

所以换句话说,我的想法是有这样的东西:

UIButton* myButton = //something

某事是我不知道该做什么的地方。有任何想法吗?我非常感谢我在这里获得的任何帮助!

4

4 回答 4

5

You need to create a property for your button in the interface:

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

And add this to implementation:

@synthesize button;

Then connect the button to it in interface builder. After this you can disable the button by:

button.enabled = NO;

Hope I could help!

于 2012-10-17T07:41:57.257 回答
1

只需为您的按钮添加标签并使用标签值访问您的按钮。

UIButton *btn = (UIButton*)[self.view viewWithTag:1];
[btn setHidden:YES];
于 2012-10-17T08:49:26.953 回答
0
@property (strong, nonatomic) UIButton *button;
@synthesize button;

// In View Did Load...
self.button = [UIButton buttonWithType:UIButtonTypeCustom]; // button can be of any type. 
[self.button setTag:1]; 
// if you have more buttons initialize it and set its tag. you can get to know which button was pressed using tags.

[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];

-(void) buttonEvent:(UIButton *) sender
{
   NSLog(@"%d",sender.tag);
    if(sender.tag == 1)
    {
      [self.button setEnabled:NO]; // This makes your button disabled, i.e you can see the button but you cannot click on it.
      [self.button setHidden:YES]; // This makes your button hidden.
     }
}

如果您有更多疑问,请回复我。

于 2012-10-18T05:18:44.123 回答
0

在您的 .h 文件中

#import <UIKit/UIKit.h>

 @interface RpViewController : UIViewController

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

 @end

在你的 .m 文件中,在实现中写下这个:

@synthesize Btn1;

Now on interface , click on button.
In button's properties - > Drawings - check   Hidden    checkbox.

Wherever you want to show that button , just write.



 [Btn1 setHidden:FALSE];
于 2012-10-17T08:10:50.097 回答