Tôi đang cố gắng sử dụng UIMenuController cho một menu động (tiêu đề và hành động đến từ một máy chủ). Vấn đề là tôi phải sử dụng UIMenuItems initWithTitle: action: nơi action là một @selector.UIMenuItems động với @selector và các phương thức động
Tôi có thể sử dụng @selector (dispatch :) nhưng sau đó tôi không thể phân biệt các mục mà người dùng đã nhấn. - (void) dispatch: (id) người gửi {NSLog (@ "% @", người gửi); } nói rằng nó là một UIMenuController và nó không có một phương pháp mà sẽ cho biết mục trình đơn đã được nhấn.
Tôi không thể chỉ viết 100 phương pháp để gửi mọi bộ chọn có thể, ok sẽ không có nhiều hơn sau đó 10 nhưng vẫn còn, điều này có vẻ không phải là một ý tưởng tốt.
Tôi có phải tạo phương pháp động cho từng bộ chọn như vậy không? http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html? Điều này có vẻ kỳ lạ quá.
Bất kỳ đề xuất nào tốt hơn sau đó là hai?
// Cách tiếp cận này không hiệu quả.
- (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
}
// Cách tiếp cận này thực sự xấu.
- (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]; }
_cmd đến từ đâu và nó là gì? – Jeena
@jen: Từ khóa cho bộ chọn có chức năng thực hiện được gọi. Thử đăng nhập 'NSStringFromSelector (_cmd)'. –
Làm việc tuyệt vời Guys, Cảm ơn bạn rất nhiều. – sachin