简体   繁体   中英

Swift 4 @IBAction UIButton action not being called correctly

I know this question is asked quite frequently on this site, but every question I have looked at all have different answers, and I have tried each of them.

I have a UIButton that I placed in my storyboard, and I connected it to an @IBAction in the view controller. Every time I click the button, it gives the error -[UIViewController openAlert]: unrecognized selector sent to instance

The function has no arguments (I have tried with the sender argument and it calls openAlertWithSender instead of openAlert(sender:) ), and I figured that connecting the button to a variable at the top of the class would work, but it gave me a new error: This class is not key value coding-compliant

I made sure the button is connected properly to the function in the storyboard and I have made sure that user interaction is enabled in the view.

I have tried building the button programmatically and it won't appear on the screen at all.

No matter what I do I can't seem to get this button to work.

My code:

class OfflineViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    // Hide the navigation bar
        navigationController?.setNavigationBarHidden(true, animated: animated)

        NetworkHandler.shared.addListener(listener: self)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Show the navigation bar
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        NetworkHandler.shared.removeListener(listener: self)

    }

    private func showMainController() -> Void {
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "NetworkAvailable", sender: self)
        }
    }

    @IBAction func openAlert() {
        let alert = UIAlertController(title: "Clicked", message: "You have clicked on the button", preferredStyle: .alert)

        self.present(alert, animated: true, completion: nil)
    }


}

Any help is appreciated.

First make sure you assigned the class name in IB to the vc OfflineViewController

Second add this method

@IBAction func printMessage(_ sender:UIButton) { } 

Instead of openAlert it seems that you changed it's name after you named it printMessage and IB encapsulates the old name that no longer exists , hence the crash

Note: storyboard is an xml file behind the scenes , so every outlet / action name persists until you change it , and there is no error mechanism to tell that when you compile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM