4

pls do not mark this question as duplicate as 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 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. Any advice pls?

Koh
  • 2,687
  • 1
  • 22
  • 62

2 Answers2

3

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
}
Codus
  • 1,433
  • 1
  • 14
  • 18
0

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.

InherentlyNuts
  • 199
  • 2
  • 8
  • I have tried various selector methods, with or without arguments, but doesn't seem to work. I also tried subclassing to UIViews too, doesn't solve the problem either. – Koh Jul 23 '17 at 10:24
  • Have u tried changeAvatarButton.addTarget(view, action: #selector(getAvatar), for: .touchUpInside) ? – InherentlyNuts Jul 24 '17 at 07:31