Skip to content

Commit ad761de

Browse files
committed
more refinements from PR jessesquires#811
1 parent c5c9c20 commit ad761de

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

JSQMessagesDemo/DemoMessagesViewController.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ - (void)viewDidLoad
7070
action:@selector(receiveMessagePressed:)];
7171

7272
/**
73-
* Register custom menu actions for cells
73+
* Register custom menu actions for cells.
7474
*/
75-
[JSQMessagesCollectionViewCell registerMenuAction:@selector(delete:)];
75+
[JSQMessagesCollectionViewCell registerMenuAction:@selector(customAction:)];
76+
[UIMenuController sharedMenuController].menuItems = @[ [[UIMenuItem alloc] initWithTitle:@"Custom Action" action:@selector(customAction:)] ];
7677

7778

7879
/**
@@ -512,9 +513,11 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
512513

513514
#pragma mark - UICollectionView Delegate
514515

516+
#pragma mark - Custom menu items
517+
515518
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
516519
{
517-
if (action == @selector(delete:)) {
520+
if (action == @selector(customAction:)) {
518521
return YES;
519522
}
520523

@@ -523,14 +526,26 @@ - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)
523526

524527
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
525528
{
526-
if (action == @selector(delete:)) {
527-
NSLog(@"Did receive delegate action %@", NSStringFromSelector(action));
529+
if (action == @selector(customAction:)) {
530+
[self customAction:sender];
528531
return;
529532
}
530533

531534
[super collectionView:collectionView performAction:action forItemAtIndexPath:indexPath withSender:sender];
532535
}
533536

537+
- (void)customAction:(id)sender
538+
{
539+
NSLog(@"Custom action received! Sender: %@", sender);
540+
541+
[[[UIAlertView alloc] initWithTitle:@"Custom Action"
542+
message:nil
543+
delegate:nil
544+
cancelButtonTitle:@"OK"
545+
otherButtonTitles:nil]
546+
show];
547+
}
548+
534549

535550

536551
#pragma mark - JSQMessages collection view flow layout delegate

JSQMessagesViewController/Views/JSQMessagesCollectionViewCell.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@
196196
*
197197
* @param action The selector to register with the cell.
198198
*
199-
* @discussion Non-Standard actions still need to be added to the `UIMenuController` manually.
199+
* @discussion Non-standard or non-system actions must be added to the `UIMenuController` manually.
200+
* You can do this by creating a new `UIMenuItem` and adding it via the controller's `menuItems` property.
200201
*
201202
* @warning Note that all message cells share the all actions registered here.
202203
*/

0 commit comments

Comments
 (0)