請不要將此問題標記爲重複,因爲我已嘗試過各種解決方案,但不起作用。我在我的FormViewController中創建了一個headerView(我正在使用Eureka),並且將我的視圖分離爲單獨的HeaderViews: NSObject
以觀察MVC原理。我試圖添加一個buttonTarget,但它會拋出錯誤unrecognized selector sent to class
。我的代碼如下:Swift:無法識別的選擇器發送到類,NSObject類中的按鈕目標
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
}()
}
}
在我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")
}
}
我不是很肯定,如果我做了任何錯誤,因爲我曾嘗試在這篇文章中的選擇的各種方法: Unrecognized selector sent to class
我懷疑它可能與子類NSObject有關。有什麼建議嗎?
你檢查[這](https://stackoverflow.com/a/35540652/1025063)? –
@Imad hm不,看不到那個帖子。好像我必須相應地重構我的代碼。謝謝。 – Koh
嘗試更改操作:#selector(getAvatar(_ :))to action:#selector(getAvatar()) –