相关疑难解决方法(0)

理解Swift 2.2选择器语法 - #selector()

我正在将我的项目语法切换到Swift 2.2(xCode帮助我自动完成); 但是,我不明白新的#selector()语法.

举个例子:

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, 
             selector: #selector(MyVC.timerCalled(_:)), //new selector syntax!
             userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

这有选择器 #selector(MyVC.timerCalled(_:))

_:意味着什么?你能在这个选择器中添加其他变量吗?说,#MyVC.timerCalled(_:whateverVar).

关于这种语法的不同之处的一般信息与早期版本的Swift中基于字符串的实现相反,我们非常感激.

selector swift swift2

36
推荐指数
4
解决办法
5万
查看次数

通过UITapGestureRecognizer选择器向函数发送参数参数

我正在使用TapGestureRecognizer创建一个具有可变数量视图的应用程序.按下视图时,我目前正在执行此操作

func addView(headline: String) {
    // ...
    let theHeadline = headline
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    // ....
}
Run Code Online (Sandbox Code Playgroud)

但在我的函数"handleTap"中,我想给它一个额外的参数(而不仅仅是发送者),就像这样

func handleTap(sender: UITapGestureRecognizer? = nil, headline: String) {
}
Run Code Online (Sandbox Code Playgroud)

如何将特定标题(每个视图都是唯一的)作为handleTap函数的参数发送?

selector uitapgesturerecognizer swift

6
推荐指数
1
解决办法
5859
查看次数

使用选择器为 UItapgestureRecognizer 传递额外的参数

我有两个标签,Label1 和 Label2。我想创建一个函数,通过为两个标签创建 UITTapRecognizer,使用传递参数的选择器调用相同的函数来打印出哪个标签被点击。下面是很长的路要走,虽然杂乱但有效。如果我知道如何将参数 (Int) 传递给选择器,它会更清晰。

let topCommentLbl1Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment1))
    topCommentLbl1Tap.numberOfTapsRequired = 2
    topCommentLbl1.userInteractionEnabled = true
    topCommentLbl1.addGestureRecognizer(topCommentLbl1Tap)

let topCommentLbl2Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment2))
        topCommentLbl2Tap.numberOfTapsRequired = 2
        topCommentLbl2.userInteractionEnabled = true
        topCommentLbl2.addGestureRecognizer(topCommentLbl2Tap)

func doubleTapTopComment1() {
    print("Double Tapped Top Comment 1")
}
func doubleTapTopComment2() {
    print("Double Tapped Top Comment 2")
}
Run Code Online (Sandbox Code Playgroud)

有没有办法修改选择器方法,这样我就可以做类似的事情

func doubleTapTopComment(label:Int) {
    if label == 1 {
        print("label \(label) double tapped")
}
Run Code Online (Sandbox Code Playgroud)

selector ios swift

4
推荐指数
1
解决办法
3670
查看次数

标签 统计

selector ×3

swift ×3

ios ×1

swift2 ×1

uitapgesturerecognizer ×1