David Seek David Seek - 7 months ago 12
Swift Question

Swift / delivery method name to IBAction to create dependency

In my example I have 3 buttons. Two of the buttons with the same responsibility level and the 3rd button has a dependency on the first ones. I want the 3rd button to call the method of either Button 1 or Button 2, depending on if Button 1 or Button 2 has been pressed.

exampleMenu.buttnOne.addTarget(self, action: #selector(ViewController.firstBtnFunc(_:)), forControlEvents: .TouchUpInside)
exampleMenu.buttnTwo.addTarget(self, action: #selector(ViewController.secondBtnFunc(_:)), forControlEvents: .TouchUpInside)


I was thinking of something like this to solve my needs:

var currentButton:String = ""

func buttnOne(button: UIButton) {
currentButton = "buttnOne"
}

func buttnTwo(button: UIButton) {
currentButton = "buttnTwo"
}

@IBAction func thirdButtonAction(sender: AnyObject) {

if currentButton == "buttnOne" {
buttnOne()
} else if currentButton == "buttnTwo" {
buttnTwo()
}
}


But that way I get the error:
Missing argument for parameter #1 in call


Using
ViewController.buttnOne()
gives me the error:
Use of instance member buttnOne on type ViewController; did you mean to use a value of type ViewController instead?


How can I solve my issue? Help is very appreciated.

Edit as requested:

enter image description here

Don't mind the interface at the moment. It's in beta and not finished yet. But it gives the idea.
I want the user to chose a category. Like liquids. And a sub-category like ML (mililiters).

The ML button has the following code:

func mlBtnFunc(button: UIButton!) {
var numberInt:Double = Double(numberStr)!

tableData = ["Mililiter", "Centiliter", "Liter", "Ounces", "Cups", "Gallons"]
tableCounterData = ["\(numberInt) ml", "\(numberInt * 0.1) cl", "\(numberInt * 0.01) l", "\(numberInt * 0.0338135) oz", "\(numberInt * 0.00422676) cups", "\(numberInt * 0.000264172) gallons"]
print(numberStr)

currentButton = "mlBtnFunc"
}


Now I want the "go" button in the keyboard section to call the function again to recalculate the value the user has entered. To make that happen, I call it now like that:

@IBAction func enterKeyboardAction(sender: AnyObject) {

if currentButton == "mlBtnFunc" {
mlBtnFunc(nil)
} else if currentButton == "clBtnFunc" {
clBtnFunc(nil)
}

}


You really can't call this best practice, but I don't know how to tell the
enterKeyboardAction
what values have to calculated... Any advice would is appreciated. But at the moment the app does, what I want her to do.

Answer

Your buttnOne and buttnTwo methods require a UIButton parameter, but you are not supplying it when you call them. If they sometimes do receive a button parameter and sometimes don't, you can declare them with an Optional UIButton parameter and call them with nil, like this:

func buttnOne(button: UIButton!) {
    currentButton = "buttnOne"
}

func buttnTwo(button: UIButton!) {
    currentButton = "buttnTwo"
}

@IBAction func thirdButtonAction(sender: AnyObject) {

    if currentButton == "buttnOne" {
        buttnOne(nil)
    } else if currentButton == "buttnTwo" {
        buttnTwo(nil)
    }
}
Comments