获取线程 1:尝试使用点击手势识别器打开时发出信号 sigabrt url
Getting thread 1: signal sigabrt when trying to use tap gesture recognizer to open url
我正在尝试使用点击手势识别器在点击图像时打开 URL。
代码构建成功,但是当我尝试点击图像时,它终止并显示
terminating with uncaught exception of type NSException (lldb)
在控制台中。控制台还显示
[Hello.ViewController clickToOpen]: unrecognized selector sent to instance.
还有一个
Thread 1: signal SIGABRT
在行
class AppDelegate: UIResponder, UIApplicationDelegate {" in 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)
}
}
}
替换您的点击手势代码 & 从 swift 3.x 您需要使用 #selector
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(clickToOpen))
self.view.addGestureRecognizer(tapGesture)
我正在尝试使用点击手势识别器在点击图像时打开 URL。 代码构建成功,但是当我尝试点击图像时,它终止并显示
terminating with uncaught exception of type NSException (lldb)
在控制台中。控制台还显示
[Hello.ViewController clickToOpen]: unrecognized selector sent to instance.
还有一个
Thread 1: signal SIGABRT
在行
class AppDelegate: UIResponder, UIApplicationDelegate {" in 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)
}
}
}
替换您的点击手势代码 & 从 swift 3.x 您需要使用 #selector
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(clickToOpen))
self.view.addGestureRecognizer(tapGesture)