Xcobe
Xcobe

Reputation: 111

iPhone/Objective C: How can I get the UIButton instance from NIB

I placed 1 UIButton on nib, and when user click this button,an IBAction method connect to it with a Status param.

What I want is, user click button and pass the Status to the IBAction method, and in that method, I need access the instance of the button.

How can I get the button's instance without an (id) sender here?

Upvotes: 0

Views: 592

Answers (4)

Nick Weaver
Nick Weaver

Reputation: 47241

You can wire up the UIButton with an outlet like you did with your IBAction to connect it.

In your header add:

UIButton *myButton;

...

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

And then link the new outlet with your button in IB.

Then you can access the button with myButton in your instance.

Upvotes: 1

PgmFreek
PgmFreek

Reputation: 6402

Create an IBOutlet variable for UIButton and connect via nib

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135548

Why without (id) sender? And what do you mean by that anyway? The sender argument of the action method is a reference to the button's instance. All you have to do is cast it to the proper type:

UIButton *button = (UIButton *)sender;

Upvotes: 0

visakh7
visakh7

Reputation: 26400

- (IBAction)buttonPress:(id)sender
{
 UIButton *button = (UIButton *)sender;
// button is the instance of your button

}

Upvotes: 0

Related Questions