1

I am very new to iPhone development. I am trying to disable an already existing button but I cant actually obtain a pointer to a specific element in the view. For instance, I have the following in the viewController header

- (IBAction)one:(id)sender; 

and the implementation is

- (IBAction)one:(id)sender { 

}

which are just event handlers. However, I need disable the button when view opens and I am a little bit lost on how to obtain references to elements outside of the event handler.

So in other words, my thought is to have something like:

UIButton* myButton = //something 

where the something is where I am lost on what to do. Any ideas? I greatly appreciate any help I get on here!

2
  • One way is to set tags to the button specific event & change when you want it.. Commented Oct 17, 2012 at 7:49
  • on interface , from button's property , make it hidden Commented Oct 17, 2012 at 8:03

4 Answers 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!

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately I cannot choose your answer for a few min, however one thing to add to this would be you need to @synthesize the variable in the implementation.Thank you very much!
1

Just give tag to your button and access your button with tag value.

UIButton *btn = (UIButton*)[self.view viewWithTag:1]; [btn setHidden:YES]; 

Comments

0

In your .h File

#import <UIKit/UIKit.h> @interface RpViewController : UIViewController @property (retain , nonatomic)IBOutlet UIButton *Btn1; @end 

In your .m file , in implementation write this :

@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]; 

Comments

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. } } 

if you have more doubts ping me back.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.