jessesquires / JSQMessagesViewController

An elegant messages UI library for iOS
https://www.jessesquires.com/blog/officially-deprecating-jsqmessagesviewcontroller/
Other
11.14k stars 2.82k forks source link

Add custom menu Action (Swift 2.0) #1282

Closed Smponias closed 9 years ago

Smponias commented 9 years ago

Hi, I have an issue that I couldn't get fixed... Maybe I am just to stupid to write the Code from Objective C in Swift or Swift 2.0 makes trouble...

I want to add a new Action to the menu then you are long pressing a cell and I figured out that it should look like this:

override func viewDidLoad() {

//Do sth JSQMessagesCollectionViewCell.registerMenuAction(Selector("customAction:")) UIMenuController.sharedMenuController().menuItems = [UIMenuItem.init(title: "customAction", action: Selector("customAction:"))] }

func customAction() { print("Button Pressed") }

I get the following error:

"fatal error: can not increment endIndex"

In DemoMessageViewController it looks like this:

override func viewDidLoad() {

/* * Register custom menu actions for cells. / [JSQMessagesCollectionViewCell registerMenuAction:@selector(customAction:)]; [UIMenuController sharedMenuController].menuItems = @[ [[UIMenuItem alloc] initWithTitle:@"Custom Action" action:@selector(customAction:)] ]; } 

Is my code wrong or is this a problem with Swift 2.0? (Swift 2.1)

Edit: I solved this error... (Just a stupid bug which just chased problems after I implemented the Action, don't know why...) But I still don't see my new custom Action. Did I forget sth to implement? I haven't found any solution yet.

Smponias commented 9 years ago

Okay I figured out that I missed some functions...

Here is my working code (Maybe it helps someone):

override func viewDidLoad(){ JSQMessagesCollectionViewCell.registerMenuAction(Selector("spam:")) UIMenuController.sharedMenuController().menuItems = [UIMenuItem.init(title: "spam", action: Selector("spam:"))] }

override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool { return true }

override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
    return action == Selector("copy:") || action == Selector("spam:")
}

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    if  action == Selector("spam:"){
        print("SPAM")
    }
}

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    if action == Selector("spam:") {
            return true
    }
    return super.canPerformAction(action, withSender:sender)
}
ArnavChawla commented 7 years ago

@Godlex I get the error Method does not override any method from it's superclass