如何在动作方法中找到被点击的按钮

问题描述

当我有一个对话框并且多个按钮具有相同的单击回调(操作方法)时,我通常想知道已按下了哪个按钮。我该怎么办?

示例:

class ButtonDialog : UIFrame{
    void button_pressed(object self){
        // how do I get this button
        TagGroup pressed_button = ?;
        result("The button " + pressed_button + " is pressed.\n");
    }
    
    object init(object self){
        TagGroup dlg,dlg_items,button1,button2;
        
        dlg = DLGCreateDialog("Press a button",dlg_items);
        
        button1 = DLGCreatePushButton("Button 1","button_pressed");
        button1.DLGIdentifier("button1");
        dlg_items.DLGAddElement(button1);
        
        button2 = DLGCreatePushButton("Button 2","button_pressed");
        button2.DLGIdentifier("button2");
        dlg_items.DLGAddElement(button2);
        
        self.super.init(dlg);
        return self;
    }
}

object dialog = alloc(ButtonDialog).init();
dialog.pose();

在我当前的程序中,我从TagGroup创建了多个行。每行都有多个按钮,但针对其特定行执行相同的操作。因此,我需要知道要修改行的按钮是哪个。 TagGroup的长度以及行数也不是固定的。因此,我无法创建button1_pressedbutton2_pressed,...函数,除非在运行过程中进行一些奇怪的事情与代码评估,我想尽可能避免这种情况。

解决方法

您不能在按钮的简单回调中传递参数,这实在令人不快。最简单的解决方案(尽管不是很优雅)是使用简单的单行回调方法,这些方法是唯一的,但它们自己调用通用方法,如下面的代码所示。

当然, mile7 指出在编译时按钮的数量不是固定的,这是一个问题。但是,除非(潜在的)按钮数量众多,否则这种方法仍然是最简单,最干净的方法,并且由于每个“硬编码”回叫只是一行,只是系统性的更改,因此使用Notepad ++或类似,以提供足够多的此类调用集。 (如果其中一些实际上从未使用过,也不会造成伤害。)

class ButtonDialog : UIFrame{
    void button_pressed(object self,string buttonID){
        // how do I get this button
        TagGroup pressed_button = self.LookUpElement(buttonID);
        if ( pressed_button.TagGroupIsValid() )
            result("The button " + buttonID + " is pressed.\n");
        else
            result("The button " + buttonID + " was not found!\n");
    }
    
    // Need to be done for each button
    void button_pressed_0(object self) { self.button_pressed("button0"); }
    void button_pressed_1(object self) { self.button_pressed("button1"); }
    
    object init(object self){
        TagGroup dlg,dlg_items
        
        dlg = DLGCreateDialog("Press a button",dlg_items);
        number nButtons = 2
        for( number n=0; n<nButtons; n++ ){
            TagGroup button = DLGCreatePushButton("Button " + n,"button_pressed_" + n);
            button.DLGIdentifier("button" + n);
            dlg_items.DLGAddElement(button);
        }
        
        self.super.init(dlg);
        return self;
    }
}

object dialog = alloc(ButtonDialog).init();
dialog.pose();
,

所以我找到了可以接受的答案,但我仍然希望获得更好的结果。

我当前的想法是使用DualStateBevelButtons。如果单击按钮,状态将更改并执行回调。然后检查所有按钮的状态是否已更改。如果是这样,这是单击的按钮,并且状态被重置。

此解决方案的非常大的缺点是,只有允许带有图像的按钮,没有文本是可能的。因此,这实际上不是通用的解决方案。

rgbimage button_img = RGBImage("button-image",4,16,16);
button_img = max(0,255 - iradius / 8 * 255);

class ButtonDialog : UIFrame{
    TagGroup buttons;
    
    void button_pressed(object self){
        for(number i = 0; i < buttons.TagGroupCountTags(); i++){
            TagGroup button;
            if(buttons.TagGroupGetIndexedTagAsTagGroup(i,button)){
                if(button.DLGGetValue() == 1){
                    // this button is toggled so it is clicked
                    string identifier;
                    button.DLGGetIdentifier(identifier);
                    
                    result("Button " + i + " (" + identifier + ") is clicked.\n");
                    
                    // reset button state
                    button.DLGBevelButtonOn(0);
                    
                    // do not continue searching,found pressed button already
                    break;
                }
            }
        }
    }
    
    object init(object self){
        TagGroup dlg,dlg_items,button1,button2;
        
        dlg = DLGCreateDialog("Press a button",dlg_items);
        buttons = NewTagList();
        
        button1 = DLGCreateDualStateBevelButton("button1",button_img,"button_pressed");
        buttons.TagGroupInsertTagAsTagGroup(infinity(),button1);
        dlg_items.DLGAddElement(button1);
        
        button2 = DLGCreateDualStateBevelButton("button2",button2);
        dlg_items.DLGAddElement(button2);
        
        self.super.init(dlg);
        return self;
    }
}

object dialog = alloc(ButtonDialog).init();
dialog.pose();

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...