Search code examples
iosswiftibaction

Swift call IBAction method not working


I am using this code to trigger a button in my view controller. The button wil be used to call the emergency services. The code doesn't seem to work.

let busPhone = 112;

@IBAction func NoodnummerCal(sender: Any) {

    if let url = NSURL(string: "tel://\(busPhone)") {
        UIApplication.shared.openURL(url as URL)
    }

When I run the app and push the button the app cashes:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ONGEVAL.Gewonden NoodnummerCal:]: unrecognized selector sent to instance


Solution

  • Your call code is working even though you´re using the old syntax. Your error is from your @IBAction. Remove that action and create a new one with the new syntax instead. Also below is an updated call code with the current syntax:

    @IBAction func callButtonClicked(_ sender: Any) {
        let busPhone = 112
        if let url = URL(string: "tel://\(busPhone)") {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }