禁用UIButton注册触摸和抛出错误

时间:2018-03-31 06:10:20

标签: ios swift xcode uibutton

简单地说,如果在选项菜单/视图(未显示)中关闭语法选项,我试图在编辑模式下禁用语法选择按钮(submitButton)。运行时,该按钮显示为已禁用,但如果您触摸该按钮则显示以下错误:

[UITextView edit]:无法识别的选择器发送到实例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 个答案:

答案 0 :(得分:1)

您已将textView指定为点按手势识别器的目标,实际上应该是self(因为edit功能已在您的{PasteView中定义1}}类,而不是UITextView)。所以这个:

UITapGestureRecognizer(target: textView, action: #selector(edit))

......应该是这样的:

UITapGestureRecognizer(target: self, action: #selector(edit))