禁用 UIButton 注册触摸并引发错误
Posted
技术标签:
【中文标题】禁用 UIButton 注册触摸并引发错误【英文标题】:Disabled UIButton registering touch and throws error 【发布时间】:2018-03-31 06:10:20 【问题描述】:简单地说,如果在选项菜单/视图(未显示)中关闭了语法选项,我正在尝试在编辑模式下禁用语法选择按钮(submitButton)。运行时,按钮显示为禁用,但如果您触摸它会显示以下错误:
[UITextView 编辑]:无法识别的选择器发送到实例 0x7fc0ff001e00
这让我很难过。我已经搜索了互联网,但我没有发现任何有用的东西。我只有两个对该按钮的引用。 IBAction 和 IBOutlet。
我相信这很简单。感觉像。 :P
请原谅凌乱的代码(也大大减少了):
编辑:添加了更多可能相关的功能。
import UIKit
import AFNetworking
import Highlightr
class PasteView: UIViewController, UITextViewDelegate, UIGestureRecognizerDelegate
var isCurrentlyEditing = false;
// Previous pastes array
var savedList: [String] = []
var submitButtonState: Bool = true
let highlightr = Highlightr()
var syntaxIndex: Int = 0
var syntaxPastebin: String = "Syntax"
var syntaxHighlightr: String = ""
var languages = ["C", "BASIC", "Java", "javascript", "Kotlin", "Swift"]
override func viewDidLoad()
super.viewDidLoad()
//Don't judge for the following code - fairly redundant but works
let tapOutTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: textView, action: #selector(edit));
textView.delegate = self;
textView.addGestureRecognizer(tapOutTextField);
view.addGestureRecognizer(tapOutTextField)
// Load previous pastes to savedList array
loadSavedListItems()
// Sets the theme of syntax highlighter. Could be made a choice in the future in Options menu.
highlightr?.setTheme(to: "github-gist")
// Picks up the default syntax/language that was set in options menu/view
let defaults = UserDefaults.standard
syntaxIndex = defaults.integer(forKey: "selectedText")
syntaxPastebin = languages[defaults.integer(forKey: "selectedText")]
syntaxHighlightr = langMap[syntaxPastebin]!
@IBOutlet weak var titleText: UITextField!
@IBAction func editAction(_ sender: Any)
titleText.text = "";
@IBOutlet weak var submitButton: UIBarButtonItem!
@IBOutlet weak var doneButton: UIBarButtonItem!
@IBAction func done(_ sender: Any)
if (!isCurrentlyEditing)
if (textView.text?.isEmpty)!
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil);
let vC: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "mainView") as! ViewController;
self.present(vC, animated: false, completion: nil);
else
let alertController = UIAlertController(title: "Are you sure?", message: "You'll lose all text currently in the editor", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "Yes", style: .default) (action) in
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil);
let vC: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "mainView") as! ViewController;
self.present(vC, animated: false, completion: nil);
alertController.addAction(OKAction)
let NoActions = UIAlertAction(title: "Cancel", style: .default) (action) in
alertController.addAction(NoActions)
self.present(alertController, animated: true)
else
isCurrentlyEditing = false;
doneButton.title = "Back";
view.endEditing(true);
submitButton.isEnabled = true
submitButtonState = true;
submitButton.title = "Submit";
// Converts pasted/typed text into highlighted syntax if selected in options menu
let defaults = UserDefaults.standard
if (defaults.object(forKey: "SyntaxState") != nil && defaults.bool(forKey: "SyntaxState") == true)
let code = textView.text
if syntaxHighlightr == "default"
textView.attributedText = highlightr?.highlight(code!)
else if syntaxHighlightr == "none"
textView.attributedText = NSAttributedString(string: code!)
else
textView.attributedText = highlightr?.highlight(code!, as: syntaxHighlightr)
@objc func edit()
isCurrentlyEditing = true
submitButtonState = false
let defaults = UserDefaults.standard
if (defaults.bool(forKey: "SyntaxState") == true)
submitButton.title = syntaxPastebin
else
submitButton.isEnabled = false
doneButton.title = "Done"
@IBOutlet weak var textView: UITextView!
@IBAction func submit(_ sender: UIBarButtonItem!)
if submitButton.isEnabled
if submitButtonState
let text = textView.text;
if (text?.isEmpty)!
let alertController = UIAlertController(title: "Error!", message: "Text cannot be empty!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) (action) in
// handle response here.
alertController.addAction(OKAction)
self.present(alertController, animated: true)
else
if (isInternetAvailable())
// Check if internet is available and then handles and submits API
else
let alertController = UIAlertController(title: "Error!", message: "Not connected to the internet!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) (action) in
// handle response here.
alertController.addAction(OKAction)
self.present(alertController, animated: true)
else
// Pops up syntax selector popup if in Editing State
selectSyntax()
// Syntax picker method with segue via code
func selectSyntax()
let sb = UIStoryboard(name: "SyntaxSelectViewController", bundle: nil)
let popup = sb.instantiateInitialViewController()! as! SyntaxSelectViewController
popup.syntax = syntaxPastebin
popup.syntaxIndex = syntaxIndex
present(popup, animated: true)
// Callback closure to fetch data from popup
popup.onSave = (data, index) in
self.syntaxHighlightr = self.langMap[data]!
self.syntaxIndex = index
self.syntaxPastebin = data
self.submitButton.title = self.syntaxPastebin
func textViewDidBeginEditing(_ textView: UITextView)
edit();
func textViewDidChange(_ textView: UITextView)
isCurrentlyEditing = true
submitButtonState = false
let defaults = UserDefaults.standard
if (defaults.bool(forKey: "SyntaxState") == true)
submitButton.title = syntaxPastebin
else
submitButton.isEnabled = false
doneButton.title = "Done"
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true
let langMap = ["C": "cpp", "BASIC": "basic", "Java": "java", "JavaScript": "javascript", "Kotlin": "kotlin", "Swift": "swift"]
【问题讨论】:
【参考方案1】:您已将textView
指定为轻击手势识别器的目标,而实际上它应该是self
(因为edit
函数是在您的PasteView
类中定义的,而不是UITextView
)。所以这个:
...应该是这样的:
UITapGestureRecognizer(目标:自我,动作:#selector(编辑))【讨论】:
以上是关于禁用 UIButton 注册触摸并引发错误的主要内容,如果未能解决你的问题,请参考以下文章