I am trying to return a value that I have made in my Xcode project. The Int value is made in the @IBAction function of a stepper.
@IBAction func stepper(_ sender: UIStepper) -> Int {
let Number: Int = Int(sender.value)
return Number
print(Number)
The system is giving me this error: "Methods declared @IBAction must return 'Void' (not'Int')".
@IBAction
is an inbuilt attribute used to fire methods that would perform certain tasks based on user's interaction and cannot return values/objects. What you can do is trigger other actions, or initialize other global/local variables within the action method.
The error - Methods declared @IBAction must return 'Void' (not'Int')
simply means that an IBAction
method cannot return anything and must return void
aka nothing.
Based on your comment on using the stepper's value for a UIButton
this is what you can do-
At the class level for the View Controller declare a variable
var stepperValue: Int = 0 {
didSet{
// use stepperValue with your UIButton as you want
}
}
And then the @IBAction
-
@IBAction func stepper(_ sender: UIStepper){
stepperValue = Int(sender.value)
}
Everytime, stepperValue
is set inside the @IBAction
method the code block inside the didSet
observer will fire and the stepperValue
's current value can be accessed inside the didSet
observer code block to be used in any logic you want there.
OR, you could simply put the entire didSet
observer block code inside of the IBAction
method stepper
.
OR, you could write another method func modifyMyButton(_ stepperval: Int)
put your logic there and call this method from inside the IBAction
method.