首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >#Selector作为参数?

#Selector作为参数?
EN

Stack Overflow用户
提问于 2018-07-10 15:55:46
回答 1查看 61关注 0票数 0

我想要创建一个将refreshControll添加到UITableView的函数。

我遇到的问题是#selector

代码语言:javascript
复制
extension UITableView{
    func refreshTableView(callFunction: #selector()){
        refreshControl = UIRefreshControl()
        refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh")
        refreshControl?.addTarget(self, action: #selector(callFunction), for: .valueChanged)
        self.addSubview(refreshControl!)

    }  
}

当我使用这个函数时,我想指定要调用哪个函数。

这有可能吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-10 15:58:37

#selector(...)Selector类型的表达式,因此您的方法应该接受Selector对象并直接将其传递给action:参数。

代码语言:javascript
复制
extension UITableView {
    func refreshTableView(callFunction: Selector) {
        // ...
        refreshControl?.addTarget(self, action: callFunction, for: .valueChanged)
        // ...
    }
}

myTableView.refreshTableView(callFunction: #selector(whatever))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51269660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档