Jazzmine
Jazzmine

Reputation: 1875

iOS7 Call an IBAction method also from ViewDidLoad

So I have a view with a button which calls an IBAction method with this method signature and it works just fine from the button.

-(IBAction)getCurrentConditions:(id)sender

I also want to call the same method from within ViewDidLoad. What do I pass in the (id)sender parameter since I don't need to pass anything from ViewDidLoad.

Upvotes: 0

Views: 597

Answers (3)

Usman Awan
Usman Awan

Reputation: 1258

Generally, IBAction methods called from .xib file but you can call it from anywhere in your implementation file by using a selector

[self performSelector:@selector(getCurrentConditions:) withObject:self.curConditionBtn afterDelay:0.0f];

Upvotes: 0

yoyosir
yoyosir

Reputation: 468

The sender is normally the 'IBOutlet' that sends the message, for example, a UIButton. So you can just link that button to your view controller and set that to be the sender. Or if you are doing nothing about the sender you can just send nil.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318934

Either pass nil or pass self.someButton if you have a property reference to the button. Of course passing nil only works if the method doesn't really need the sender. Then again, if the method doesn't need the sender, why bother having the parameter at all?

[self getCurrentCondition:self.someButton];

or

[self getCurrentCondition:nil];

Or change the signature to:

- (IBAction)getCurrentCondition {
    // Do stuff that doesn't need the sender
}

Upvotes: 0

Related Questions