ios swift3中的选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38038418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:34:06  来源:igfitidea点击:

Selector in swift3

iosselectorswift3

提问by Chris

Why doesn't this work in swift 3 ? It crashes at runtime saying:

为什么这在 swift 3 中不起作用?它在运行时崩溃说:

'-[my_app_name.displayOtherAppsCtrl tap:]: unrecognized selector sent to instance 0x17eceb70'

'-[my_app_name.displayOtherAppsCtrl tap:]: 无法识别的选择器发送到实例 0x17eceb70'

    override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    //self.collectionView!.register(ImageCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.

  let lpgr = UITapGestureRecognizer(target: self, action: Selector("tap:"))
    lpgr.delegate = self
    collectionView?.addGestureRecognizer(lpgr)
}

func tap(gestureReconizer: UITapGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.ended {
  return
}

let p = gestureReconizer.location(in: self.collectionView)
let indexPath = self.collectionView?.indexPathForItem(at: p)

if let index = indexPath {
  //var cell = self.collectionView?.cellForItem(at: index)
  // do stuff with your cell, for example print the indexPath
  print(index.row)
} else {
  print("Could not find index path")
}
}

回答by jjatie

Selector("tap:")should now be written as #selector(tap(gestureReconizer:))

Selector("tap:")现在应该写成 #selector(tap(gestureReconizer:))

Also, you should declare tap as func tap(_ gestureRecognizer: UITapGestureRecognizer)as per the new Swift API Guidelinesin which case your selector would then become #selector(tap(_:)).

此外,您应该func tap(_ gestureRecognizer: UITapGestureRecognizer)根据新的Swift API 指南声明 tap,在这种情况下,您的选择器将变为#selector(tap(_:)).

回答by Neen

In Swift 3 it works like this:

在 Swift 3 中,它是这样工作的:

@IBOutlet var myView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap))

    myView.addGestureRecognizer(tap)
}

func handleTap() {
    print("tapped")
}

回答by Zeeshan

Swift 3 came with new syntax so instead of using Selector("tap:"), #selector(tap(gestureReconizer:)) is

Swift 3 带来了新的语法,而不是使用 Selector("tap:"),#selector(tap(gestureReconizer:)) 是

回答by GeekMeng

Swift 3:

斯威夫特 3:

class MYPTempController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.addSubview(btn)
        btn.addTarget(self, action: #selector(MYPTempController.btnClick), for: .touchUpInside)
    }
    @objc fileprivate func btnClick() {
        print("--click--")
    }
}


//带参数
btn.addTarget(self, action: #selector(MYPTempController.btnClick(_:)), for: .touchUpInside)
//监听方法
func btnClick(_ sender: UIButton) {
    print("--click--")
}