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
Passing Data Between 2 Uiviewcontroller Using Delegate and Protocol
Swift - Pushviewcontroller from Appdelegate, Rootviewcontroller.Navigationcontroller Is Nil
Using Generic In-App Purchase Items for a Dynamic Range of Digital Products
Attribute Unavailable Warning on iOS Versions Prior to 8.0
Uitableviewcell Height Resize When Image Is Downloaded
What's Currently the "Correct" Way to Set a Uiview's Corner Radius
Afnetworking - Do Not Cache Response
How to Upload Video to Server from Iphone
Can Swift Closures Be Set to a Default Value When Used as a Parameter in a Function
Xcode/Swift 'Filename Used Twice' Build Error
Flutter:Not Connecting to iOS Simulator in Android Studio
Move Xcode Project Causes Red Text (Missing Files from Project)
Uiview/Calayer: Transform Triggers Layoutsubviews in Superview
Malloc Error "Can't Allocate Region" Failed with Error Code 12. Any Idea How to Resolve This
How to Easily Support Light and Dark Mode with a Custom Color Used in My App