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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With