Estoy tratando de usar UIMenuController para un menú dinámico (los títulos y las acciones provienen de un servidor). El problema es que tengo que usar UIMenuItems initWithTitle: action: donde action es un @selector.UIMenuItems dinámicos con @selector y métodos dinámicos
Puedo usar @selector (dispatch :) pero no puedo distinguir cuáles de los elementos presionó el usuario. - (void) dispatch: (id) remitente {NSLog (@ "% @", remitente); } dice que es un UIMenuController y no tiene un método que indique qué elemento del menú se presionó.
No puedo escribir 100 métodos para enviar todos los posibles selectores, vale, no habrá más de 10, pero aún así, esto no parece una buena idea.
¿Tengo que crear métodos dinámicos para cada uno de esos selectores? http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html? Esto parece extraño también.
¿Alguna mejor propuesta que estas dos?
// Este enfoque no funciona.
- (void)showMenu {
[self becomeFirstResponder];
NSMutableArray *menuItems = [[NSMutableArray alloc] init];
UIMenuItem *item;
for (MLAction *action in self.dataSource.actions) {
item = [[UIMenuItem alloc] initWithTitle:action.title action:@selector(action:)];
[menuItems addObject:item];
[item release];
}
UIMenuController *menuController = [UIMenuController sharedMenuController];
menuController.menuItems = menuItems;
[menuItems release];
[menuController update];
[menuController setMenuVisible:YES animated:YES];
}
- (void)action:(id)sender {
NSLog(@"%@", sender); // gives UIMenuController instead of UIMenuItem
// I can not know which menu item was pressed
}
// Este enfoque es realmente feo.
- (void)showMenu {
[self becomeFirstResponder];
NSMutableArray *menuItems = [[NSMutableArray alloc] initWithCapacity:5];
UIMenuItem *item;
NSInteger i = 0;
for (MLAction *action in self.dataSource.actions) {
item = [[UIMenuItem alloc] initWithTitle:action.text
action:NSSelectorFromString([NSString stringWithFormat:@"action%i:", i++])];
[menuItems addObject:item];
[item release];
}
UIMenuController *menuController = [UIMenuController sharedMenuController];
menuController.menuItems = menuItems;
[menuItems release];
[menuController update];
[menuController setMenuVisible:YES animated:YES];
}
- (void)action:(NSInteger)number {
NSLog(@"%i", number); // gives the index of the action in the menu.
}
// This is a hack, I have to assume that there will never be more then 15 actions
- (void)action0:(id)sender { [self action:0]; }
- (void)action1:(id)sender { [self action:1]; }
- (void)action2:(id)sender { [self action:2]; }
- (void)action3:(id)sender { [self action:3]; }
- (void)action4:(id)sender { [self action:4]; }
- (void)action5:(id)sender { [self action:5]; }
- (void)action6:(id)sender { [self action:6]; }
- (void)action7:(id)sender { [self action:7]; }
- (void)action8:(id)sender { [self action:8]; }
- (void)action9:(id)sender { [self action:8]; }
- (void)action10:(id)sender { [self action:10]; }
- (void)action11:(id)sender { [self action:11]; }
- (void)action12:(id)sender { [self action:12]; }
- (void)action13:(id)sender { [self action:13]; }
- (void)action14:(id)sender { [self action:14]; }
¿De dónde viene el _cmd y qué es? – Jeena
@jen: palabra clave para el selector con el que se invoca la función de implementación. Intente registrar 'NSStringFromSelector (_cmd)'. –
Awesome work Chicos, muchas gracias. – sachin