Pass Different Parameters to an Ibaction

Multiple Parameters on an IBAction Function

IBAction methods cannot have arbitrary signatures. You cannot add an extra parameter here. There's no way for the button to send you this (how does the button know what place is?) Generally this is handled either by there only being one UI element pointing to this action (so you know what button was pressed), or using a tag on the sender to identify it. Every view has a tag property that's just an integer. You can set that in Interface Builder or in code, and then you can read it to identify the sender.

Start by reading over Target Action in the docs that explains how this works on the various platforms. In general, the signature of an IBAction must be:

@IBAction func action(_ sender: Any)

However, on iOS, it may also be:

@IBAction func action(_ sender: Any, forEvent: UIEvent)

As Taylor M points out below, you can also have use this signature (though I can't remember right off hand if this works outside of iOS; I've only personally used it there).

@IBAction func action()

But that's it. There are no other allowed signatures.

Pass different parameters to an IBAction

The so called "IBActions" must have one of these signatures:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

You cannot add any other parameters. Nevertheless you can use sender (which is button1 or button2 in your case) to get the parameter:

-(void)actionWithSender:(UIButton*)sender {
NSString* parameter;
if (sender.tag == 1) // button1
parameter = @"foo";
else // button2
parameter = @"bar";
...
}

Passing a Swift variable from one IBAction to another

So the variables are not going to be PASSED to Submit() because that's the function the button calls. The button can careless about your two variables. Instead you'll want to have two Global variables (in this case the two random numbers) and request them in Submit. These are variables that was created outside any functions and are set when they need to be set. So lets see how you'd do that with your code:

// Two random numbers (global)
var a: Int?
var b: Int?

func generateRandomNumber() {
a = // assign a to the number generator value
b = // same as above
}

func submit() { // your button call
if let userGuess = text.text { // do if let instead of ! it's safer
let total = a + b

if userGuess == total { // our check remember == is different from =
correctView.hidden = false
} else { // if it's wrong show this view
tryAgainView.hidden = false
}
}
}

Obviously there's a bunch missing but you can get the main idea. Let me know if these anything I can answer about this method.

How to Auto call an @IBAction function

You can either change the signature of the IBAction by making its parameter an Optional like this:

@IBAction func doSomeTask(sender: UIButton?) {
// code
}

and then call it with nil as an argument:

doSomeTask(nil)

Or you can use the IBAction as a wrapper for the real function:

func doSomeTaskForButton() {
// ...
}

@IBAction func doSomeTask(sender: UIButton) {
doSomeTaskForButton()
}

meaning you can then call doSomeTaskForButton() from wherever you want.



Related Topics



Leave a reply



Submit