10

動的メニューにUIMenuControllerを使用しようとしています(タイトルとアクションはサーバーから取得されます)。問題は、UIMenuItems initWithTitle:action:を使用する必要があることです。ここで、actionは@selectorです。

@selector(dispatch :)を使用できますが、ユーザーがどのアイテムを押したかを区別できません。-(void)dispatch:(id)sender {NSLog(@ "%@"、sender); }は、それがUIMenuControllerであり、どのメニュー項目が押されたかを通知するメソッドがないことを示しています。

可能なすべてのセレクターをディスパッチするために100個のメソッドを作成することはできません。10個を超えることはありませんが、それでも、これは良い考えではないようです。

そのようなセレクターごとに動的メソッドを作成する必要がありますか?http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html?これも奇妙に思えます。

この2つよりも良い提案はありますか?

//このアプローチは機能しません。

- (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
}

//このアプローチは本当に醜いです。

- (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]; }
4

2 に答える 2

11

このアプローチは機能しますが、ボタンごとに一意のセレクター名と、その名前からターゲットにするものへのマッピングが必要です。
セレクター名には、一意の文字列を選択する必要があります(UUID、またはタイトルのサニタイズおよびプレフィックスバージョンが機能する可能性があります)。次に、呼び出しを解決し、異なるセレクター名で「エイリアス」する1つのメソッドが必要です。

- (void)updateMenu:(NSArray *)menuEntries {
    Class cls = [self class];
    SEL fwd = @selector(forwarder:);
    for (MenuEntry *entry in menuEntries) {
        SEL sel = [self uniqueActionSelector];
        // assuming keys not being retained, otherwise use NSValue:
        [self.actionDict addObject:entry.url forKey:sel]; 
        class_addMethod(cls, sel, [cls instanceMethodForSelector:fwd], "v@:@");
        // now add menu item with sel as the action
    }
}

これで、フォワーダーはメニュー項目に関連付けられているURLを検索できます。

- (void)forwarder:(UIMenuController *)mc {
    NSLog(@"URL for item is: %@", [actionDict objectForKey:_cmd]);
}

セレクターを生成するには、次のようなものを使用できます。

- (SEL)uniqueActionSelector {
    NSString *unique = ...; // the unique part
    NSString *selString = [NSString stringWithFormat:@"menu_%@:", unique];
    SEL sel = sel_registerName([selString UTF8String]);
    return sel;
}
于 2010-07-14T20:51:30.117 に答える
0

メニュー項目が同じことをしない限り、なぜそれらはアクションを共有する必要がありますか?先に進んで、必要な動作を指定するアクションを記述し、メニュー項目をそれらにリンクします。

于 2010-07-14T20:26:27.183 に答える