Koh
Koh

Reputation: 2897

Swift: Unrecognized selector sent to class, buttons target in NSObject class

I have tried the various solutions but doesn't quite work. I am creating a headerView in my FormViewController (I'm using Eureka) and I separated my views into a separate HeaderViews: NSObject to observe the MVC principle.

I attempted to add a buttonTarget but it throws the error unrecognized selector sent to class. My code is as follows:

class ProfileSettingsViewController: FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        form +++
            
            Section(){ section in
                section.header = {
                    var header = HeaderFooterView<UIView>(.callback({
                        let view = HeaderViews.setUpHeaderForProfileView(user: self.user)
                        return view
                    }))
                    header.height = { 200 }
                    return header
                }()
            }
        }

And in my HeaderViews:

class HeaderViews: NSObject {
    
    static func setUpHeaderForProfileView(user: User) -> UIView {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screen.width, height: 100))
        let changeAvatarButton = UIButton(frame: CGRect(x: (screen.width / 2) - 50, y: avatarImageView.frame.maxY + 10, width: 100, height: 20))
        changeAvatarButton.addTarget(self, action: #selector(getAvatar(_:)), for: .touchUpInside)
        view.addSubview(changeAvatarButton)
        return view
    }

    func getAvatar(_ sender: UIButton!) {
        print("Change Avatar tapped")
    }
}

I'm not quite sure if I have made any mistake, as I have tried various methods of the selectors in this post: Unrecognized selector sent to class

I suspect it might something to do with the subclass NSObject. What can I try next?

Upvotes: 4

Views: 2470

Answers (2)

Codus
Codus

Reputation: 1473

Update If you could not get instance of HeaderViews. then just change getAvatar to static funciton.

static func getAvatar(_ sender: UIButton!) {
    print("Change Avatar tapped")
}

Origin You should not use self as target in static function. Because this self means HeaderViews. Passing instance to function as target.

Ex:

static func setUpHeaderForProfileView(user: User, in instance: HeaderViews) -> UIView {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: screen.width, height: 100))
    let changeAvatarButton = UIButton(frame: CGRect(x: (screen.width / 2) - 50, y: avatarImageView.frame.maxY + 10, width: 100, height: 20))
    changeAvatarButton.addTarget(instance, action: #selector(getAvatar(_:)), for: .touchUpInside)
    view.addSubview(changeAvatarButton)
    return view
}

Upvotes: 3

InherentlyNuts
InherentlyNuts

Reputation: 199

If you are not using the UIButton Parameter, define func getAvatar() with no parameters or func getAvatar(_ sender: AnyObject?)

The function call would change to this. changeAvatarButton.addTarget(self, action: #selector(getAvatar), for: .touchUpInside)

Also I am curious why not define HeaderViews as a subclass of UIView?

That might actually be the problem here.

Upvotes: 0

Related Questions