1

I am using JSQMessages to build a chat app. I am attempting to turn off the "backspace" in the UITextView element by overriding the deleteBackward() function. I could hack at the JSQ framework core, which I want to avoid, and I've tried forced downcasting, but that didn't work either. How can I override a function of an instance of a class (UITextView) that is referenced deep down the tree of the current class (JSQMessagesViewController).

Here is the gist of the code:

In my ChatViewController.swift file

class ChatViewController: JSQMessagesViewController, UICollectionViewDataSource, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    ....

    override func viewDidLoad() {
    super.viewDidLoad()

    var user = PFUser.currentUser()
    self.senderId = user.objectId
    self.senderDisplayName = user[PF_USER_FULLNAME] as! String

    //here's the important line
    let base = self.inputToolbar.contentView.textView
    let newtexview = base as! UITextViewNB

    .....        

}

And in my keyboard.swift file

class UITextViewNB: UITextView {

    override func deleteBackward() {
        // blank because I just want to shut it off
    }
}

I'm clearly missing something conceptually.

1 Answers1

0

The as Family of operators don't actually change the class of the underlying object. They just tell the compiler to generate code to insure that the object class is already correct and then treat the object as the new type for future code generation. Just because you cast a String to an Int doesn't actually change anything about the String.

I'm not familiar with the framework you give, but one thing you could try is to get the existing textView, make a new UITextViewNB configured the same, and then remove the existing UITextView and replace with the new one. Don't forget to assign your new text view to ....contentView.textView. It's not at all clear that that would work, or even that contentView.textView can be assigned.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Thanks for the suggestion. The assignment is done inside the framework, which means hacking core. I'm trying to avoid recasting several classes and files just for one override function - but that may be the only way forward. – jamesvillarrubia Sep 17 '15 at 16:07