3

I am new to iOS development and I am working on UIMenuController. It looks like we need to have a different selector for each UIMenuItem.

Is there a way to have a single selector and determine which item I clicked?

Can we send an argument to the selector so that we can identify which item we clicked?

This is the way i am initializing the menu item.

UIMenuItem *item = [[UIMenuItem alloc]initWithTitle:@"Item 1" action:@selector(itemClicked:)];
rahul
  • 6,447
  • 3
  • 31
  • 42
  • 1
    Although not in the docs, I suspect `@selector(itemClicked:)` will have its argument set to the `UIMenuItem` instance which is clicked. Could you try that and report back if it's the case? –  Jun 11 '13 at 05:30
  • I've tried this, but the argument is set to UIMenuController. I don't think i can use that to identify the clicked item. – rahul Jun 11 '13 at 05:53
  • Unfortunately. Then you don't really have any other option. –  Jun 11 '13 at 05:58
  • if there is no other way, then it is awful... what if i need some 30 menu items... define 30 selectors? I may have to look for an alternative... – rahul Jun 11 '13 at 06:09
  • I start to feel that this is an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What exactly are trying to do? –  Jun 11 '13 at 06:10
  • (And as a last resort, you might try to subclass `UIMenuController` and set a property on it each time a menu item is pressed, but I don't think you should be doing that.) –  Jun 11 '13 at 06:11
  • 1
    http://stackoverflow.com/questions/9146670/ios-uimenucontroller-uimenuitem-how-to-determine-item-selected-with-generic-sel –  Jun 11 '13 at 06:23
  • @H2CO3: My requirement is simple. I have a UITableView with each row as a category. When I click any row, A menu should pop up and that menu is different for each row. So, I need to identify which menu item I have clicked as I have to take a different action based on that each item. – rahul Jun 11 '13 at 06:23
  • @sathvik That's similar to what I was talking about, thanks. –  Jun 11 '13 at 06:25
  • @rahul Take a look at the solution Sathvik linked. –  Jun 11 '13 at 06:25

1 Answers1

1

You can use blocks to handle the delegation like this

UIMenuItem.h

@property (nonatomic, copy) void (^onButtonClicked)(id btn);

UIMenuItem.m

@synthesize onButtonClicked;

- (IBAction)btnExpandClicked:(id)sender{

  self.onButtonClicked(sender);
}

which will be connected to each menu item

then in you UITableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath {
...
item.onButtonClicked = ^(id btn) {
        // code that will run when the menu item is clicked "not the row of the table"
        // btn is the menu button clicked, you can implement a pop up based on each menu button clicked ( based on the tag for example )
    };
Orfeas
  • 9
  • 2