Cannot Use parameter for Selector in Swift Class

I don’t actually get an error, but the buttons don’t call the function when they get pressed. I created a class to make my buttons easier but there is an issue with the selector. Right now all this code is in my viewController at the moment.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class NavButton {
var button: UIButton = UIButton(type: UIButtonType.custom)
init(iconName: String, selector: Selector ){
self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
self.button.addTarget(self, action: selector, for: .touchUpInside)
self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
}
func construct() -> UIButton {
return self.button
}
}
private func setupNavBar(){
let searchButton = NavButton(iconName:"search_icon", selector: #selector(handleSearch))
let moreButton = NavButton(iconName:"nav_more_icon", selector: #selector(handleMore))
let searchBarButtonItem = UIBarButtonItem(customView: searchButton.construct())
let moreBarButtonItem = UIBarButtonItem(customView: moreButton.construct())
navigationItem.rightBarButtonItems = [
moreBarButtonItem,
searchBarButtonItem
]
}
@objc func handleSearch(){
print("search click")
}
@objc func handleMore(){
print("more click")
}
</code>
<code>class NavButton { var button: UIButton = UIButton(type: UIButtonType.custom) init(iconName: String, selector: Selector ){ self.button.setImage(UIImage(named: iconName), for: UIControlState.normal) self.button.addTarget(self, action: selector, for: .touchUpInside) self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20) self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true } func construct() -> UIButton { return self.button } } private func setupNavBar(){ let searchButton = NavButton(iconName:"search_icon", selector: #selector(handleSearch)) let moreButton = NavButton(iconName:"nav_more_icon", selector: #selector(handleMore)) let searchBarButtonItem = UIBarButtonItem(customView: searchButton.construct()) let moreBarButtonItem = UIBarButtonItem(customView: moreButton.construct()) navigationItem.rightBarButtonItems = [ moreBarButtonItem, searchBarButtonItem ] } @objc func handleSearch(){ print("search click") } @objc func handleMore(){ print("more click") } </code>
class NavButton {
    var button: UIButton = UIButton(type: UIButtonType.custom)

    init(iconName: String, selector: Selector ){
        self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
        self.button.addTarget(self, action: selector, for: .touchUpInside)
        self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
        self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
        self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
    }

    func construct() -> UIButton {
        return self.button
    }

}


private func setupNavBar(){

    let searchButton = NavButton(iconName:"search_icon", selector: #selector(handleSearch))
    let moreButton = NavButton(iconName:"nav_more_icon", selector: #selector(handleMore))
    let searchBarButtonItem = UIBarButtonItem(customView: searchButton.construct())
    let moreBarButtonItem = UIBarButtonItem(customView: moreButton.construct())

    navigationItem.rightBarButtonItems = [
        moreBarButtonItem,
        searchBarButtonItem
    ]
}

@objc func handleSearch(){
    print("search click")
}

@objc func handleMore(){
    print("more click")
}

Selectors only represent a method, but they don’t represent on which object to call the method i.e. the target.

In your constructor, you set the target to self:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>self.button.addTarget(self, action: selector, for: .touchUpInside)
^^^^^
</code>
<code>self.button.addTarget(self, action: selector, for: .touchUpInside) ^^^^^ </code>
self.button.addTarget(self, action: selector, for: .touchUpInside)
                      ^^^^^

This means that the button, when pressed will try to call an instance method in the NavButton class. But you actually want to call the handleSearch method in some other class!

To fix this, add a target parameter in the constructor:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>init(iconName: String, target: Any?, selector: Selector ){
self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
// note the change to the below line
self.button.addTarget(target, action: selector, for: .touchUpInside)
self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
}
</code>
<code>init(iconName: String, target: Any?, selector: Selector ){ self.button.setImage(UIImage(named: iconName), for: UIControlState.normal) // note the change to the below line self.button.addTarget(target, action: selector, for: .touchUpInside) self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20) self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true } </code>
init(iconName: String, target: Any?, selector: Selector ){
    self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
    // note the change to the below line
    self.button.addTarget(target, action: selector, for: .touchUpInside)
    self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
    self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
}

And then call the constructor like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NavButton(iconName:"search_icon", target: self, selector: #selector(handleSearch))
</code>
<code>NavButton(iconName:"search_icon", target: self, selector: #selector(handleSearch)) </code>
NavButton(iconName:"search_icon", target: self, selector: #selector(handleSearch))

0

Pass the target as a parameter into the NavButton class.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>init(iconName: String, target: Any?, selector: Selector){
button.setImage(UIImage(named: iconName), for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
// Rest of method follows
}
</code>
<code>init(iconName: String, target: Any?, selector: Selector){ button.setImage(UIImage(named: iconName), for: .normal) button.addTarget(target, action: selector, for: .touchUpInside) // Rest of method follows } </code>
init(iconName: String, target: Any?, selector: Selector){
    button.setImage(UIImage(named: iconName), for: .normal)
    button.addTarget(target, action: selector, for: .touchUpInside)
    // Rest of method follows
}

The target should be the object on which the action selector is defined.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật