Serguei Fedorov
Serguei Fedorov

Reputation: 7923

Get UIButton reference from viewDidLoad

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!

Upvotes: 1

Views: 654

Answers (4)

Vinod ram
Vinod ram

Reputation: 95

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

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31081

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

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

Upvotes: 1

Curious_k.shree
Curious_k.shree

Reputation: 1020

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

Upvotes: 0

romeouald
romeouald

Reputation: 186

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!

Upvotes: 5

Related Questions