0

I believe I've followed the steps of other SO threads to invoke an action, via another method, when the UIBarButtonItem is tapped however I keep getting the following error Argument of '#selector' does not refer to an '@objc' method, property, or initializer and the error is highlighting the #selector text.

This is setting up the button:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "\(barButtonItemText)", style: .done, target: self, action: #selector(inviteMethod(inviteMethod: inviteMethodSelected!)))

This is the method I'm trying to run when the button is tapped:

    func inviteMethod (inviteMethod:String) {

    let inviteMethod = inviteMethod

    if inviteMethod == "email" {
        inviteViaEmail(invitedContacts: self.invitedContacts)
    }
    else {
        inviteViaText(invitedContacts: self.invitedContacts)  
    }  }

Example of one of the invite methods in case it's helpful

    func inviteViaEmail (invitedContacts:[Contact]) {

    var invitedContactsEmails:[String] = []

    for contactEmails in invitedContacts {

        let emailAddress = contactEmails.emailAddress ?? ""

        invitedContactsEmails.append(emailAddress)

    }

        let mc = MFMailComposeViewController()
        mc.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate

        mc.setToRecipients(invitedContactsEmails)
    mc.setSubject("Invite for Gallery App, Event \(event?.eventName)")

    mc.setMessageBody("This is a test invite email \n\n\(event?.eventHost) has invited you to join their event", isHTML: false)

        self.present(mc, animated: true, completion: nil)

}
akash23a
  • 127
  • 10

1 Answers1

3

An action of UIBarButtonItem – as well as every other UIControl object – can take one parameter which is the object itself or no parameter.

The associated method is either

@objc func inviteMethod()

or

@objc func inviteMethod(_ sender : UIBarButtonItem)

Any custom parameters are not supported


And you have to init the button this way:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "\(barButtonItemText)", 
                                                    style: .done, 
                                                    target: self, 
                                                    action: #selector(inviteMethod))
vadian
  • 274,689
  • 30
  • 353
  • 361