代码之家  ›  专栏  ›  技术社区  ›  J Fernandes

获取线程1:尝试使用点击手势识别器打开url时发出sigabrt信号

  •  0
  • J Fernandes  · 技术社区  · 7 年前

    我正在尝试使用点击手势识别器在点击图像时打开URL。 代码成功构建,但当我尝试点击图像时,它终止并显示

    以NSException(lldb)类型的未捕获异常终止

    [Hello.ViewController clickToOpen]:发送到实例的无法识别的选择器。

    还有一个

    排队

    类AppDelegate:UIResponder,UIApplicationDelegate{”,在AppDelegate.swift中。

    ViewController代码如下:

    //
    //  ViewController.swift
    //  Hello
    //
    
    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
    
    //MARK: Outlets
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var helloName: UILabel!
    @IBOutlet weak var imageToTap: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        nameTextField.delegate = self
    
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("clickToOpen")))
        self.imageToTap.addGestureRecognizer(tapGestureRecognizer)
        self.imageToTap.isUserInteractionEnabled = true
    }
    
    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard
        textField.resignFirstResponder()
    
        return true 
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        helloName.text = "Hello \(textField.text!)"
        textField.text = ""
    }
    
    //MARK: Actions
    @IBAction func clearButton(_ sender: UIButton) {
        helloName.text = "Hello"
    }
    @IBAction func enterButton(_ sender: UIButton) {
        nameTextField.resignFirstResponder()
    }
    @IBAction func clickToOpen(_ sender: UITapGestureRecognizer) {
         if let url = NSURL(string: "http://www.google.com/") {
            UIApplication.shared.openURL(url as URL)
        }
    
    }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Prashant Tukadiya    7 年前

    #selector

        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(clickToOpen))
        self.view.addGestureRecognizer(tapGesture)