only instance methods can be declared @IBAction building audio app

54 Views Asked by At

I've been trying to build an IOS app that will allow me to click a button and have audio play and I'm getting stuck on this error only instance methods can be declared @IBAction. I've had a search and it doesn't seem like I've closed it out of the class or anything like that but it still doesn't want to work

import UIKit
import AVFoundation
            
class LevelAStartingSoundViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


@IBAction func Sbutton(_ sender: Any, forEvent event: UIEvent) {
        playSound(sound: "Ssound", type: "wav")
    
    
    }
}
}
1

There are 1 best solutions below

2
aheze On

If you reformat your code (Ctrl + i), you'll notice that Sbutton is inside viewDidLoad.

Function method is inside viewDidLoad method

You need to move it outside.

@IBAction func sButton(_ sender: UIButton) { /// make sure you have the right function signature
    playSound(sound: "Ssound", type: "wav")
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}