views:

278

answers:

2

I'm currently calling an action method in an object by dragging a button to the view in interface builder. I then drag an object to the panel and specify my object with the action method. I shift drag my button to the object and choose my action method. This works in calling my method.

I would like to instead call my action method without interface builder from uiview class that the current nib is derived from.

Does anyone know how I can accomplish this?

+5  A: 

If your action method is defined like:

- (IBAction)myMethod:(id)sender;

(the sender parameter is entirely optional), then you can just call

[self myMethod:self];

IBAction is simply a macro defined to allow Interface Builder to pick up on methods that can be used in Interface Builder. It resolves to void, after preprocessing, so your method signature at runtime is:

- (void)myMethod:(id)sender;

Just like any other method.

Jasarien
+2  A: 

I read the question differently than Jasarien from the first answer.

If you are trying to get a button to respond to a click, but you want to setup the selected target method without using IB, it would go like this.

[myButton addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
NWCoder