Objective c 打开URL的Swift 2.3按钮失败,错误为无法识别选择器

Objective c 打开URL的Swift 2.3按钮失败,错误为无法识别选择器,objective-c,swift,uibutton,openurl,addtarget,Objective C,Swift,Uibutton,Openurl,Addtarget,我创建了一个打开url链接的按钮,但由于无法识别的选择器错误而失败。我通常会通过在线阅读的方式将add target设置为self,但对于这个特定的实例,我会得到错误: 无法将NSOBJECT->()->infoViewcontroller.infoview类型的值转换为预期的参数类型AnyObject。 为了避免这个问题,Xcode建议将目标设置为NSOBJECT.self。但是,这不再得到错误,而是在单击按钮时崩溃,并返回[NSObject displayWebLink]:发送到类0x106

我创建了一个打开url链接的按钮,但由于无法识别的选择器错误而失败。我通常会通过在线阅读的方式将add target设置为self,但对于这个特定的实例,我会得到错误: 无法将NSOBJECT->()->infoViewcontroller.infoview类型的值转换为预期的参数类型AnyObject。 为了避免这个问题,Xcode建议将目标设置为NSOBJECT.self。但是,这不再得到错误,而是在单击按钮时崩溃,并返回[NSObject displayWebLink]:发送到类0x106011e58的无法识别的选择器的原因。所以我只是想知道这样做的正确方法是什么,为什么?下面是我的代码

 class ActivityInfoView: UICollectionViewCell {


    var activity: ActivitysEvents? {

        didSet {
                       }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var textView: UITextView = {
        let tv = UITextView()
        tv.userInteractionEnabled = false
        return tv
    }()

    let dividerLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGrayColor()
        return view

    }()

    let urlLinkButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.redColor()
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.blueColor(), forState: .Normal)
       // button.addTarget(self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
        button.addTarget(NSObject.self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
        return button

    }()

   func displayWebLink() {
         print("abcdefghijklmnop")
    if let urlLink = activity?.url {
          //  UIApplication.sharedApplication().openURL(NSURL(string: urlLink)!)
            UIApplication.sharedApplication().openURL(NSURL(string:  urlLink)!, options: [:], completionHandler: nil)
           print("dhudududuhdu")
    }
    }

`这不是一条非常有用的错误消息。编译器正在尝试验证是否正确的对象定义了名为
displayWebLink
的方法,并且它似乎正在使用闭包类型作为搜索的上下文

尝试告诉它在哪里可以找到该方法:

button.addTarget(self, action: #selector(ActivityInfoView.displayWebLink), forControlEvents: .TouchUpInside)

我通过将按钮从“let”更改为“lazy var”修复了此问题,如下所示:

    lazy var urlLinkButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.redColor()
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        button.addTarget(self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
        //button.addTarget(self(), action: #selector(ActivityInfoView.displayWebLink), forControlEvents: .TouchUpInside)
        return button

    }()

按钮.addTarget(NSObject.self,操作:#选择器(displayWebLink),forControlEvents:.TouchUpInside)
更改为
按钮.addTarget(self,操作:#选择器(displayWebLink),forControlEvents:.TouchUpInside)
这就是我之前遇到的问题,它给了我一个错误“无法转换NSOBJECT类型的值…”仍然会因为相同的错误而崩溃,我相信这与self的使用有关,原因是它不允许我使用self,所以我必须在它的位置使用NSOBJECT。